Programmatically defining a new navigation controller order/stack?

前端 未结 2 785
误落风尘
误落风尘 2020-12-22 09:12

I have a NavigationController that the following VC\'s are embedded in: VC1 -> VC2 -> VC3 -> VC4 -> VC5. My problem is that when I segue from VC5 (after editin

2条回答
  •  忘掉有多难
    2020-12-22 09:49

    The best way to do this is via an unwind segue.

    In VC3 you define an appropriate unwind function:

    @IBAction func unwind(segue:UIStoryboardSegue) {
        if let sourceViewController = segue.sourceViewController as? VC5 {
             let myNewData=sourceViewController.someProperty
             self.someFunctionThatUpdatesScene()
    }
    

    Then in the VC5 scene you can create an unwind segue in one of two ways.

    If you want it to be triggered directly from an object, such as a UIButton, you drag from the action in the inspector to the exit icon at the top of the scene and select unwind from the pop up.

    If you want to trigger the unwind programatically then you drag from the view controller object in the explorer on the left to the exit icon and select unwind from the popup. You will now see an unwind segue in the explorer and you can give it an identifier like you would with any segue. You can use this identifier with performSegueWithIdentifier

    The advantage of this approach is that you don't need to make any assumptions about the depth of UINavigationController stack and you don't need to implement a delegate/protocol to pass data back.

    Apple has a very good Tech Note on Using Unwind Segues

提交回复
热议问题