No history for a screen on the ios

 ̄綄美尐妖づ 提交于 2021-02-10 06:33:52

问题


I'm using monotouch for developing ios applications.Suppose that I have 3 screens: s1, s2 and s3. User can open them like this: s1 -> s2 -> s3 .

I want that if user open s2 it can back by navigation back button to the s1 screen, but if it goes to the s3 screen by pressing back button in the navigation bar the application back to the s1 no s2.

Is it possible in the ios? On the android if you use NoHistory attribute you can implement this feature, is there any equivalent in the ios too?


回答1:


You can manually edit the view controllers in your navigationController to remove s2.

You can use self.navigationController.viewControllers to get the current stack, and [self.navigationController setViewControllers:animated:] to set the stack.

In your case, you might want to do:

NSMutableArray *views = [[NSMutableArray alloc] initWithArray:self.navigationController.viewControllers];
[views removeObjectAtIndex:1];
[self.navigationController setViewControllers:views animated:NO];

If you call this in your s3 viewController, it will get the viewControllers on the stack, remove the second one (object at index 1), and set them as the new stack. animated:NO just makes it so that it does it instantly without animation.

Be aware that you might want to change this line of code so that if you ever get to s3 without s2 being before it, it does not pop s3. Also if there is another view between s2 and the root, the one you want to remove might not be objectAtIndex:1.




回答2:


U can try this

On Back Button Press:

for (UIViewController * viewController in self.navigationController.viewControllers)
{
    if ([viewController isKindOfClass: NSClassFromString(@"<ClassName for S3>")])
    {
         [self.navigationController popToViewController: [self.navigationController.viewControllers objectAtIndex: 0] animated: NO];
          break;
    }
}

or u can also use

   [self.navigationController popToRootViewControllerAnimated: NO];


来源:https://stackoverflow.com/questions/18048095/no-history-for-a-screen-on-the-ios

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