2016-04-16 43 views
2

我試圖弄清楚很長時間。有人能告訴我爲什麼我的委託方法從未被調用。它的tvOS項目,但我相信它應該作爲簡單的iOS應用程序。點擊按鈕我有一個彈出的表格視圖和選擇我想要更新按鈕標籤與選定的選項。代表返回nil tvOS

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 
    } 

} 
+0

你有沒有按Ctrl +拖動或點擊右鍵暫停+拖動從的tableView到ViewController設置委託在故事板? –

+0

是的,我已經做了 –

+0

其中委託方法沒有得到一個調用,updateSelected或tableView的委託方法? –

回答

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 
    } 

和解決的問題yeeee :)

1

的問題是,你得到你的委託作爲零,因爲可以呈現一個時刻只有一個視圖控制器。由於你的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()。那麼只有你能夠更新它的屬性,也就是從月份標籤。

+0

popupViewController的是一個彈出窗口,我已經解僱了,無需導航到VacationPlannerController。我剛剛在vacationplanner中添加了prepareForSegue,它工作正常! –

+0

@RichaSrivastava Ohk很好,那是什麼要求!但是,如果能夠幫助您找到問題,您是否可以提出答案,以便其他人也可以理解問題,儘管您需要的解決方案由您提供。 –