Ios 委托返回nil tvOS
我正试图从很长一段时间里找出答案。有人能告诉我为什么从未调用我的委托方法吗。这是一个tvOS项目,但我相信它应该像简单的iOS应用程序一样工作。点击按钮后,我会弹出一个表格视图,点击“选择”,我会尝试用所选选项更新按钮标签Ios 委托返回nil tvOS,ios,swift,Ios,Swift,我正试图从很长一段时间里找出答案。有人能告诉我为什么从未调用我的委托方法吗。这是一个tvOS项目,但我相信它应该像简单的iOS应用程序一样工作。点击按钮后,我会弹出一个表格视图,点击“选择”,我会尝试用所选选项更新按钮标签 protocol PopupSelectionHandlerProtocol{ func UpdateSelected(data:String) } class PopupViewController: UIViewController, UITableViewDa
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
}
}
问题是,您将代理设置为nil,因为一次只能显示一个ViewController。因为没有加载popupViewController的视图。未调用viewDidLoad()方法,导致未设置popupDelegate 如果要检查其空值。在你的电脑里试试这个。。。方法
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标签 解决这个问题有两件事- PopupViewController中的第一个- 在didSelectRowAtIndexPath中,替换
delegate?.UpdateSelected((tableView.cellForRowAtIndexPath(indexPath)?.textLabel?.text)!)
与
第二位是度假计划控制员-
从viewDidLoad中删除以下代码-
let popupDelegate = PopupViewController()
popupDelegate.delegate = self
并增加了PrepareForegue-
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let destinationVC = segue.destinationViewController as! PopupViewController
destinationVC.delegate = self
}
问题已解决(yeeee:)您是否通过按住Ctrl键并拖动或右键单击按住并拖动将代理从tableView设置到ViewController?是的,我已设置,哪个代理方法未获得调用,updateSelected或tableView的代理方法?updateSelected未被调用。。。我正在获取已加载数据的弹出表视图,但在选择任何一行时,我尝试调用updateSelected delegate方法,该方法不起作用。您是否收到对DidSelectRowatineIndexPath的调用:?popupViewController是一个弹出窗口,我不需要导航到VacationPlannerController。我刚刚在vacationplanner中添加了prepareForSegue,它成功了@RichaSrivastava Ohk很好,这就是我们需要的!但是,如果答案有助于你发现问题,请你投票,以便其他人也能理解问题,尽管你需要的解决方案是由你提出的。
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let destinationVC = segue.destinationViewController as! PopupViewController
destinationVC.delegate = self
}