簡體   English   中英

Swift Delegate返回零

[英]Swift Delegate return nil

我嘗試從UITableViewCell調用UIAlertController。

我有我的代表

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

我在下面的TableViewCell類中調用了。

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)
    }
}

為了呈現視圖控制器,我使用DEPFlightStripCellDelegate將其設置在mainView類中,並調用我在“callAlert”上面聲明的函數來顯示警報。

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

但是代表返回零。 任何人都知道為什么?

mainView實例尚未分配的delegateDEPFlightStripCell 當您實例化該單元格時,通常在表視圖委托方法中,您應該為該單元格賦予其委托。

就像是:

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
  }

  // ...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM