我已阅读 this thread (和其他类似的)从下到上,但它根本不符合我的需求。
我在 UINavigationController
中的 UIPageViewController
中有一个 UIViewController
。导航到第二个 ViewController。导航到第三个 ViewController 并想返回到第二个 ViewController 传送数据。
我目前的代码:
protocol PassClubDelegate {
func passClub(passedClub: Club)
}
class My3rdVC: UIViewController {
var clubs: [Club] = []
var passClubDelegate: PassClubDelegate?
....
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let club = clubs[indexPath.row]
self.passClubDelegate?.passClub(club)
navigationController?.popViewControllerAnimated(true)
}
我的第二个风投:
class My2ndVC: UIViewController, PassClubDelegate {
var club = Club()
func passClub(passedClub: Club) {
SpeedLog.print("passClub called \(passedClub)")
club = passedClub
}
passClub
未被调用。我确定这是因为我没有将委托(delegate)设置为 My2ndVC,但我该怎么做呢?我发现的所有解决方案都希望我使用 a) segue 或 b) 实例化一个新的 My2ndVC,这没有任何意义,因为它仍在内存中,我想弹出回到层次结构中。我错过了什么?我的可能性是什么?非常感谢您的帮助。
PS:我没有使用任何转场。 My3rdVC 被调用:
let vc = stb.instantiateViewControllerWithIdentifier("My3rdVC") as! My3rdVC
self.navigationController?.pushViewController(vc, animated: true)
最佳答案
可以在My2ndVC
的prepareForSegue
方法中设置My3rdVC
的delegate。
class My2ndVC: UIViewController, PassClubDelegate {
...
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
super.prepareForSegue(segue, sender: sender)
switch segue.destinationController {
case let controller as My3rdVC:
controller.passClubDelegate = self
}
}
}
这是假设您在 Storyboard中创建了一个 segue,将 My3rdVC
从 My2ndVC
推送到导航 Controller 堆栈,我假设您有。因此,只需尝试将此 prepareForSegue
方法简单地粘贴到 My2ndVC
中,看看它是否有效。
更新
let vc = stb.instantiateViewControllerWithIdentifier("My3rdVC") as! My3rdVC
vc.passClubDelegate = self
navigationController?.pushViewController(vc, animated: true)
关于ios - Swift/如何使用 popViewController 调用委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39692791/