【发布时间】:2016-08-22 12:10:51
【问题描述】:
在下面的代码中,我有一个操作/按钮,当点击它时会将您带到不同的 viewController (SecondViewController),一切正常,但有一行代码我不完全理解。
这行代码在做什么?
secondVC.delegate = self
我们在这里谈论的是什么代表?如果我只需要转到其他视图并且不传递任何数据,那真的需要吗?
@IBAction func goToView(sender: AnyObject) {
let secondVC= storyboard?.instantiateViewControllerWithIdentifier("secondViewController") as! SecondViewController
secondVC.delegate = self // is this needed?
self.presentViewController(secondVC, animated:true, completion:nil)
}
【问题讨论】:
-
您是否在第二个视图控制器中创建了自定义委托?
-
只是移动到 SecondViewController 那行代码不是必需的。如果你已经在你的类中实现了委托方法并且需要从 secondViewController 调用,那么你需要设置
-
presentViewController 中必须有应该从 secondViewController 调用的协议。禁用 secondVC.delegate = 如果不使用协议,则自身不会受到伤害
-
有道理,如果我理解正确的话,只有在使用委托模式来传递数据时才需要这样做。