Clearing back stack on transition

时间秒杀一切 提交于 2020-01-06 21:14:22

问题


I have a storyboard set up using modal transitions as follows:

root -> A -> B -> C

When I transition from B to C, I want to clear the backstack because a user will never be able to get back to A, B or root. I know it is not possible to remove the root view controller but is there a way to remove A and B from the stack when transition to C.

I have Android background and in there it can be done by simply adding CLEAR_TOP and NEW_TASK flag to the intent before starting next activity. Is there something similar in iOS?

I am trying to call this on the on the transition from B->C but it crashes the app.

[self dismissViewControllerAnimated:NO completion:nil];

Also tried this on view did Load of B but it doesn't work.

[self.presentingViewController dismissViewControllerAnimated:NO completion:nil];

EDIT: Is it possible to make C the root and clear A->B and previous root. How would you do that?


回答1:


All you have to do is set yourself as the window's root view controller in the viewDidAppear method of C.

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.view.window.rootViewController = self;
}

Root, A, and B will be deallocated when you do this.




回答2:


Maybe you can use UINavigationController to achieve what you want. After B->C, just clear the controller stack. You can custom the transition style of UINavigationController like this:

CATransition *animation = [CATransition animation];
[animation setDuration:0.3];
[animation setType: kCATransitionMoveIn];
[animation setSubtype: kCATransitionFromTop];
[animation setTimingFunction:[CAMediaTimingFunction   functionWithName:kCAMediaTimingFunctionDefault]];

[nav pushViewController:yourViewController animated:NO];

[nav.view.layer addAnimation:animation forKey:nil];



回答3:


Have you tried -removeFromSuperview




回答4:


One approach you can use is:

root-> A -> B -> C -> (fakeRoot) => (real root)

Before leaving root, you can create a snapshot of the root (like this)

Then you can use your fancy modal transition to transition to the "fake root" but put the real root behind it, in the type of controller you want for moving forward. Then just remove the fake root to expose the real one.

  • Make a viewController called "fakeRoot" controller that was just a big imageView that fills the screen
  • Set the imageView to the image of "root" that you created with the snapshot above.
  • do your modal transition to this imageView
  • pop to the root of your nav controller without animation (that should make root appear, but you won't notice because your modally presented view will looks just like it)


来源:https://stackoverflow.com/questions/17905552/clearing-back-stack-on-transition

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