Multiple unwind in the same button - Swift

2019-08-01 06:39发布

I would like to know if it is possible to assign two different unwind method at the same button. For example:

I have this views navigations:

  • Home->A->B
  • Home->C->B

A and C views navigates to B, but then I want to return to previous views using the same B view and controller.

It is possible?

I have been thinking about write assign unwind method to the button programmatically depending what view comes.

Thanks in advance

I'm sorry about my english, is not good.

2条回答
啃猪蹄的小仙女
2楼-- · 2019-08-01 07:14

Although, question isn't very clear. But what I could understand is that you want to navigate back to the previous view i.e. B>C or B>A depending upon where user came from.

If so, then check the UINavigationController. It keeps track of the navigation history and automatically adds a back button. Kind of like the back button in our browsers.

Here is a tutorial, although a bit old: Link

查看更多
甜甜的少女心
3楼-- · 2019-08-01 07:23

Here's a Swift solution that worked well for me. The code below only works if you hookup your segues correctly in the storyboard and in code. Checkout this page for great explanations on setting up unwind segues.

In summary:

  1. You're accessing the same view from multiple other views. So, when you segue to a view, you can pass the source view controller (the view that you're currently in) to a property in the view that you're going to.

  2. In your view that you will unwind out of, you can check the property holding the info (the class) on where you came from, and then perform a segue based on what view it is.

The code: (using ex: Home -> A -> B or... Home -> C -> B)

Note: B is the view that will unwind to multiple different views.

In A or C: (code works the same way in both views)

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "segueIdentifierInViewAthatGoesToViewB" {  
        let controller:B = segue.destinationViewController as! B
        //the code below passes our current view controller class to a property in view B. 
        //So, view B will know what view we came from. 
        //In our example, we're coming from view A or C
        controller.viewControllerNavigatedFrom = segue.sourceViewController
    }
}

In B:

//setup an IBAction that will check the source view controller (that we passed when we segued to this view) and perform a segue based on where we came from. You can hook this up to a button or anything you want really.

//isKindOfClass(A) - "A" would be the name of your class

//setup a property to receive the view controller class where we are coming from
var viewControllerNavigatedFrom:AnyObject?

@IBAction func myButtonPressed(sender: AnyObject) {
    if self.viewControllerNavigatedFrom!.isKindOfClass(A) {
        //Unwind to view A
        performSegueWithIdentifier("unwindFromBbackToA", sender: sender)
    }
    else if self.viewControllerNavigatedFrom!.isKindOfClass(C) {
        //Unwind to view C
        performSegueWithIdentifier("unwindFromBbackToC", sender: sender)
    }
}
查看更多
登录 后发表回答