Swift /如何使用popViewController调用委托

我已经从下到上阅读了这个主题 (和类似的其他主题),但是这根本不符合我的需求。

我有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) } 

我的第二个VC:

 class My2ndVC: UIViewController, PassClubDelegate { var club = Club() func passClub(passedClub: Club) { SpeedLog.print("passClub called \(passedClub)") club = passedClub } 

passClub不叫。 我敢肯定,这是因为我没有将代理设置为My2ndVC,但我该怎么做呢? 我find的所有解决scheme都希望我使用a)segue或b)实例化一个My2ndVC new,它没有任何意义,因为它仍然在内存中,我想popup回到层次结构。 我错过了什么? 我有什么可能? 帮助非常感谢。

PS :我没有使用任何赛段。 My3rdVC被调用者:

 let vc = stb.instantiateViewControllerWithIdentifier("My3rdVC") as! My3rdVC self.navigationController?.pushViewController(vc, animated: true) 

您可以在My3rdVCprepareForSegue方法中设置My2ndVC

 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 } } } 

假设你已经在故事板中创build了一个将My3rdVCMy2ndVC推到导航控制器堆栈上的segue,我假设你有这个堆栈。 所以试试把这个prepareForSegue方法粘贴到My2ndVC ,看看它是否有效。

UPDATE

 let vc = stb.instantiateViewControllerWithIdentifier("My3rdVC") as! My3rdVC vc.passClubDelegate = self navigationController?.pushViewController(vc, animated: true)