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

前端 未结 6 1311
一向
一向 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:33

    Your question was really interesting to me, because I never used IB and segues before (don't judge me for that) and wanted to learn something new. As you described in your comments:

    viewDidAppear will be called on B when C rewinds to A

    So I come up with an easy custom solution to this:

    protocol ViewControllerSingletonDelegate: class {
    
        func viewControllerWillUnwind(viewcontroller: UIViewController, toViewController: UIViewController)
    }
    
    class ViewControllerSingleton {
    
        static let sharedInstance = ViewControllerSingleton()
    
        private var delegates: [ViewControllerSingletonDelegate] = []
    
        func addDelegate(delegate: ViewControllerSingletonDelegate) {
    
            if !self.containsDelegate(delegate) {
    
                self.delegates.append(delegate)
            }
        }
    
        func removeDelegate(delegate: ViewControllerSingletonDelegate) {
    
            /* implement any other function by your self :) */
        }
    
        func containsDelegate(delegate: ViewControllerSingletonDelegate) -> Bool {
    
            for aDelegate in self.delegates {
    
                if aDelegate === delegate { return true }
            }
    
            return false
        }
    
        func forwardToDelegate(closure: (delegate: ViewControllerSingletonDelegate) -> Void) {
    
            for aDelegate in self.delegates { closure(delegate: aDelegate) }
        }
    }
    
    
    class SomeViewController: UIViewController, ViewControllerSingletonDelegate {
    
        let viewControllerSingleton = ViewControllerSingleton.sharedInstance
    
        func someFunction() { // some function where you'll set the delegate
    
            self.viewControllerSingleton.addDelegate(self)
        }
    
        /* I assume you have something like this in your code */
        @IBAction func unwindToSomeOtherController(unwindSegue: UIStoryboardSegue) {
    
            self.viewControllerSingleton.forwardToDelegate { (delegate) -> Void in
    
                delegate.viewControllerWillUnwind(unwindSegue.sourceViewController, toViewController: unwindSegue.destinationViewController)
            }
    
            /* do something here */
        }
    
        // MARK: - ViewControllerSingletonDelegate
        func viewControllerWillUnwind(viewcontroller: UIViewController, toViewController: UIViewController) {
    
            /* do something with the callback */
            /* set some flag for example inside your view controller so your viewDidAppear will know what to do */
        }
    }
    

    You also could modify the callback function to return something else, like controller identifier instead the controller itself.

    I do everything programmatically, so please don't judge me for that too. ;)

    If this code snippet won't help you, I'd still love to see some feedback.

提交回复
热议问题