Swift Delegate返回零

时间:2015-04-09 14:56:37

标签: ios uitableview ipad swift uialertcontroller

我尝试从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)
  }
}

然而,代表返回零。任何人都知道为什么?

1 个答案:

答案 0 :(得分:2)

您的mainView实例尚未被指定为delegate中的DEPFlightStripCell。当您实例化该单元格时,通常在表视图委托方法中,您应该为该单元格赋予其委托。

类似的东西:

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
  }

  // ...
}