How can i navigate to mainviewcontroller from subview

末鹿安然 提交于 2019-12-02 09:33:09
NRitH

Are you using storyboards, or is this all in code? If it's in code, create a method like

func goToMainVC() {
    if let navController = self.navigationController {
        navController.popToRootViewControllerAnimated(true)
    }
}

and set the button's target to a selector that calls it.

If you're using storyboards, you have three options:

  1. ctrl+drag a connection from your button back to the main view controller (easy, but bad form because it just pushes the main VC back onto the nav controller);
  2. Add @IBAction before the fun goToMainVC() method above, then ctrl+drag a connection from your button to the view controller in which it's contained, and then select that outlet method (this is how most people would do it); or
  3. The best option is to use an unwind segue, as described here.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!