In my viewDidAppear, how do I know when it's being unwound by a child?

前端 未结 6 1324
一向
一向 2021-01-01 13:36

When my child performs an unwind segue, my controller\'s viewDidAppear gets called.

In this method (and this method alone, I need to know whether it was from an unwi

6条回答
  •  旧巷少年郎
    2021-01-01 14:14

    Suppose the segue navigation is ViewController -> FirstViewController -> SecondViewController. There is an unwind from SecondViewController to ViewController. You can add in the intermediary FirstViewController the following code to detect unwind actions.

    import UIKit
    
    class FirstViewController: UIViewController {
    
        var unwindAction:Bool = false
        override func viewDidAppear(animated: Bool) {
            if unwindAction {
                println("Unwind action")
                unwindAction = false
            }
        }
        override func viewControllerForUnwindSegueAction(action: Selector, fromViewController: UIViewController, withSender sender: AnyObject?) -> UIViewController? {
            self.unwindAction = true
    
             return super.viewControllerForUnwindSegueAction(action, fromViewController: fromViewController, withSender: sender)
        }
    
    }
    

    EDIT After giving this some thought, I decided the solution to this depends on the kind of complexity that you are dealing with here. What exactly do you do when you do the unwind segue? The solutions given here are viable and they work -- only if you want to detect whether it is an unwind action. What if you want to pass the data between the point where the unwind is happening to the root? What if there is a complex set of preparations that you wanna do in one of the intermediate view controllers? What if you want to do both of these?

    In such complex scenarios, I would immediately rule out overriding the unwind methods of the view controller. Doing such operations there will work, but it won't be clean. A method will be doing what it isn't supposed to do. Smell that? That's code smell.

    What if, somehow a view controller could inform the next view controller in the hierarchy of the event happening? Better yet, how do we do this without tightly coupling these two?

    Protocol.

    Have a protocol definition something like:

    protocol UnwindResponding {
        prepareForUnwindSegue(segue:UISegue , formViewController:UIViewController, withImportantInfo info:[String,AnyObject])
    }
    

    Using protocol you will keep the relationship between the objects -- the hierarchy of view controllers in this case -- explicit. At the point of occurrence of a particular event, you will delegate the call to the next controller in the hierarchy informing about the happening of a particular event in another view controller. Here is an example:

    override func prepareForSegue(segue:UIStoryboardSegue, sender:AnyObject?) {
    
        if let unwindResponder = self.presentingViewController as? UnwindResponding where segue.identifier = "unwindSegue" {
            unwindResponder.prepareForUnwindSegue(segue:UISegue, fromViewController:self,info:info)
        }
    
    
    }
    

    In the intermediary view controller you can do something like:

    extension IntermediaryViewController : UnwindResponding {
        prepareForUnwindSegue(segue:UISegue , fromViewController:UIViewController, withImportantInfo info:[String,AnyObject]) {
            if let unwindResponder =  self.presentingViewController {
                unwindResponder.prepareForUnwindSegue(segue,fromViewController:fromViewController, info:info)
            }
            unwindSegue = true
        }
    }
    

    Granted, you wouldn't wanna do this if you just want to detect unwind segues. Maybe you do, you'll never know what will happen in the future. Never hurts to keep your code clean.

提交回复
热议问题