【问题标题】:Swift Delegate return nilSwift 代表返回 nil
【发布时间】:2015-04-09 14:56:37
【问题描述】:

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

但是,委托返回 nil。有人知道为什么吗?

【问题讨论】:

    标签: ios uitableview ipad swift uialertcontroller


    【解决方案1】:

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

    类似:

    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
      }
    
      // ...
    }
    

    【讨论】:

      猜你喜欢
      • 2018-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多