1

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.

vicmator
  • 23
  • 5

2 Answers2

1

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)
    }
}
Community
  • 1
  • 1
0

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

Taha Samad
  • 1,105
  • 1
  • 9
  • 21
  • I wanted to do with unwind segue because is easier and cleaner programmatically, but I think that I will do in the way that UINavigationContoller documentation said. Thanks very much. – vicmator Mar 03 '15 at 08:21