Multiple unwind in the same button - Swift

久未见 提交于 2019-12-13 20:01:56

问题


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.


回答1:


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




回答2:


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


来源:https://stackoverflow.com/questions/28809350/multiple-unwind-in-the-same-button-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!