Dismiss or remove previous modally presented view controller as soon as the next one appear modally

后端 未结 3 682
谎友^
谎友^ 2021-01-05 06:22

My target include a lot view need to present different view modally base on each user action. Here what I want to do to get cleaner view hierarchy and better user experience

3条回答
  •  不知归路
    2021-01-05 06:46

    What you want is an "unwind segue":

    https://developer.apple.com/library/archive/featuredarticles/ViewControllerPGforiPhoneOS/UsingSegues.html#//apple_ref/doc/uid/TP40007457-CH15-SW8

    https://developer.apple.com/library/archive/technotes/tn2298/_index.html

    It allows you to dismiss multiple view controllers at the same time, without having to know how many there are in the stack.

    In VC1 you would implement an IBAction called (for instance) unwindToRoot. Then in the storyboard for VC3, you wire up your Done button to the Exit object and choose the unwindToRoot action.

    When that button is pressed, the system will dismiss all the view controllers it needs to bring you back to VC1.

    This is better than calling presentingViewController?.presentingViewController?.dismiss(), because VC3 doesn't need to know anything about the view controller hierarchy underneath it.

提交回复
热议问题