2015-04-09 3 views
3

Я пытаюсь вызвать UIAlertController из UITableViewCell.Swift Delegate return nil

У меня есть делегат

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

Чтобы представить контроллер представления я установить его внутри моего MainView класса с DEPFlightStripCellDelegate и вызвать функцию я декларировать выше «callAlert», чтобы отобразить предупреждение.

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

Однако делегат возвращает ноль. У кого-нибудь есть идея, почему?

ответ

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 
    } 

    // ... 
} 
Смежные вопросы