Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Delegate return nil

I try to call the UIAlertController out of an UITableViewCell.

I have my delegate

protocol DEPFlightStripCellDelegate {
  func callAlert(AlertCon: UIAlertController!)
}

whitch I called inside of my TableViewCell Class below.

class DEPFlightStripCell: UITableViewCell {
    var delegate: DEPFlightStripCellDelegate?

    @IBAction func changeClearedFlightLevel(sender: UIButton) {
        let AddAlert: UIAlertController = UIAlertController(title: "Select Altitude", message: "", preferredStyle: .Alert)
        self.delegate?.callAlert(AddAlert)
    }
}

To present the view controller I set it up inside of my mainView class with DEPFlightStripCellDelegate and call the function I declare above "callAlert" to display the alert.

class mainView: UIViewController,UITextFieldDelegate, DEPFlightStripCellDelegate {
  func callAlert(AlertCon: UIAlertController!) {
    println("Test")
    self.presentViewController(AlertCon, animated: true, completion: nil)
  }
}

However the delegate return nil. Anyone have an idea why?

like image 339
Moritz Avatar asked Jul 15 '26 17:07

Moritz


1 Answers

Your mainView instance has not been assigned as the delegate in a DEPFlightStripCell. When you instantiate that cell, usually in a table view delegate method, you should give that cell its delegate.

Something like:

class mainView: UIViewController,UITextFieldDelegate, DEPFlightStripCellDelegate {
  // ...

  override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) 
       as! DEPFlightStripCell

    cell.delegate = self
    return cell
  }

  // ...
}
like image 63
GoZoner Avatar answered Jul 17 '26 13:07

GoZoner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!