2016-09-25 72 views
2

我已经从下到上阅读了this thread(以及类似的其他),但它根本不符合我的需求。Swift /如何使用popViewController调用委托

我有一个UIViewController里面UIPageViewControllerUINavigationController内。导航到第二个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,但我该怎么做?我找到的所有解决方案都希望我使用a)segue或b)实例化一个My2ndVC new,它没有任何意义,因为它仍然在内存中,我想弹回来重新回到层次结构中。我错过了什么?我有什么可能?非常感谢帮助。

PS:我没有使用任何segues。 My3rdVC被称为是:

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

回答

3

您可以在My2ndVCprepareForSegue方法设置的My3rdVC委托。

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

这是假设你已经在你的故事板,从My2ndVCMy3rdVC到导航控制器栈,我假设你已经创建了一个SEGUE。所以试试把这个prepareForSegue方法粘贴到My2ndVC,看看它是否有效。

UPDATE

let vc = stb.instantiateViewControllerWithIdentifier("My3rdVC") as! My3rdVC 

vc.passClubDelegate = self 

navigationController?.pushViewController(vc, animated: true) 
+0

我没有使用任何塞格斯。 –

+0

但当然,为你的努力upvote。谢谢 –

+0

你可以在实例化My3rdVC之后传递委托,但在把它推到导航控制器堆栈 – Callam