代表返回nil tvOS

时间:2016-04-16 20:49:54

标签: ios swift

我想从很长一段时间内弄清楚。有人能告诉我为什么我的委托方法永远不会被调用。它是一个tvOS项目,但我相信它应该像简单的iOS应用程序一样工作。点击按钮我有一个弹出表视图,然后在选择我试图用选定的选项更新按钮标签。

protocol PopupSelectionHandlerProtocol{
    func UpdateSelected(data:String)
}

class PopupViewController: UIViewController, UITableViewDataSource,UITableViewDelegate {

    @IBOutlet weak var myTable: UITableView!

    let months = [1,2,3,4,5,6,7,8,9,10,11,12]
    let days = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31]
    let yearsRange = [2015,2016,2017,2018,2019,2020]
    var popupType:String!
    var delegate:PopupSelectionHandlerProtocol?
    override func viewDidLoad() {
        super.viewDidLoad()
        popupType = "months"
    }


    func numberOfSectionsInTableView(tableView: UITableView) -> Int {

        return 1
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if (popupType == "months"){
            return 12
        }else if (popupType == "days"){
            return 31
        }else if (popupType == "years")
        {
            return 6
        }
        return 10
    }

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

        cell.textLabel?.text = String(months[indexPath.row])
        return cell

    }
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        print(tableView.cellForRowAtIndexPath(indexPath)?.textLabel?.text)
        delegate?.UpdateSelected((tableView.cellForRowAtIndexPath(indexPath)?.textLabel?.text)!)
        self.dismissViewControllerAnimated(true, completion: nil)
    }


}

然后这个 -

class VacationPlannerController: UIViewController,PopupSelectionHandlerProtocol {

    @IBOutlet weak var fromMonth: UIButton!
    override func viewDidLoad() {
        super.viewDidLoad()
        let popupDelegate = PopupViewController()
        popupDelegate.delegate = self
    }

    func UpdateSelected(data:String){
        print("Inside UpdateSelected VacationPlannerController \(data)")
        fromMonth.titleLabel?.text = data
    }

}

2 个答案:

答案 0 :(得分:1)

问题是,你的代理是零,因为一次只能有一个ViewController。由于未加载popupViewController的视图。未调用viewDidLoad()方法,导致未设置popupDelegate。

如果要检查其无效性。在你的didSelect ...方法

中试试这个
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    self.navigationController?.pushViewController(VacationPlannerController(), animated: true)
    if(delegate==nil){
        print("delegate is nil")
    }
    delegate?.UpdateSelected((tableView.cellForRowAtIndexPath(indexPath)?.textLabel?.text)!)
}

如果您想要更新fromMonth按钮。首先,您需要提交/推送VacationPlannerController以调用其viewDidLoad()。然后,只有您可以更新其属性,即fromMonth标签。

答案 1 :(得分:0)

解决此问题的两件事 -

首先在PopupViewController- 在didSelectRowAtIndexPath中,替换了

delegate?.UpdateSelected((tableView.cellForRowAtIndexPath(indexPath)?.textLabel?.text)!)

self.delegate?.UpdateSelected((tableView.cellForRowAtIndexPath(indexPath)?.textLabel?.text)!)

VacationPlannerController中的第二位 - 从viewDidLoad -

中删除了以下代码
    let popupDelegate = PopupViewController()
    popupDelegate.delegate = self

并添加了prepareForSegue -

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        let destinationVC = segue.destinationViewController as! PopupViewController
        destinationVC.delegate = self
    }

问题已经解决了。)