简体   繁体   中英

iOS: Dismiss view controller and push new view without showing the current view in navigation stack

I have a navigation controller stack with different possible controllers in it.

I want to present a view controller modally as and when I need it based on the next view I am pushing in to navigation controller. (You can think of that as user login validation screen..which validates user before moving to next screen.)

I am able to do it successfully with

UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(contactPicker, animated: true, completion: nil)

and dismiss it when I need to dismiss with

UIApplication.sharedApplication().keyWindow?.rootViewController?.dismissViewControllerAnimated(false, completion: {
        self.navigationController.pushViewController(myNewVC, animated: true)
    })

I am able to do it however the transition from modally presented view to my (myNewvC) new push for next view is not exactly how i want it to be. When view dismiss happen I can see my caller screen for a second before i move to next view. I don't want to see that screen. Is it possible?

(Also I don't want to add validation screen in navigation stack as purpose of that screen is not to be part of navigation stack).

The completion block is exactly added here by Apple to protect you from uncaught exceptions because what you want to do might result in one, cause you can't do two animations at the same time, maybe some suggested solutions:

Try to present the new controller then when it's done hide the previous one in the completion block (this way when the user dismisses the new controller, he won't see the previous one), eg Assuming you are in VC1 one now and you want to dismiss it and show VC2, you can use the below code in objective-c

[self presentViewController:VC2 animated:YES completion:^{
    [self dismissViewControllerAnimated:YES completion:nil];
}];

In swift

self.presentViewController(VC2, animated: true) { () -> Void in
    self.dismissViewControllerAnimated(true, completion: nil)
}

Or you can just ignore it if the user won't dismiss the new controller, then he would never see the old one but I don't think this is memory-efficient because the controller would be stuck in memory.

I didn't try it myself but if you use UINavigationViewController and you just don't want to go back that ViewController, than you can simple remove from stack after you go to the next ViewController. In here you can see how you can change the UINavigationViewController's ViewController array: Removing viewcontrollers from navigation stack

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM