How to Pop stack in IOS?

旧时模样 提交于 2019-12-13 04:55:37

问题


I am building an iOS application.

What I am doing is explained below -

  1. There are 3 ViewControllers named as A, B, C.
  2. I go to A to B and then to C on button click function.
  3. I have to Implement Navigation controller on all three ViewController (A, B, C).
  4. Now, when I press back of navigation controller the stack that is followed is C -> B -> A. This is the default behavior.

What I need is the stack operation should be perform like - C -> A.

For this I have to Pop B from the stack. How can I Pop the B from stack.


回答1:


If your intention is to always go back to the first view controller on the stack, you can use

[self.navigationController popToRootViewControllerAnimated:YES];

If you want to pop X number of view controllers, check this answer from SO: How do I pop two views at once from a navigation controller?




回答2:


use below code to do this

NSArray *viewContrlls=[[self navigationController] viewControllers];
for( int i=0;i<[ viewContrlls count];i++){
  id obj=[viewContrlls objectAtIndex:i];
  if([obj isKindOfClass:[A class]]){
    // A is your class where to popback
    [[self navigationController] popToViewController:obj animated:YES];
    return;
    }
  }


来源:https://stackoverflow.com/questions/30304026/how-to-pop-stack-in-ios

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