ios iPhone - 删除呈现的视图控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18230254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iPhone - Removing a presented view controller
提问by Carmichael
On a view controller I have a button that will present another view controller. From the second view controller, I can go to other view controllers, but not necessarily back to the one that got me here. If this is the case, how can I remove the original view controller?
在视图控制器上,我有一个按钮可以显示另一个视图控制器。从第二个视图控制器,我可以转到其他视图控制器,但不一定回到让我来到这里的那个。如果是这种情况,如何删除原始视图控制器?
回答by mbpro
Your description is a bit unclear here. There could be 3 different cases here:
你的描述在这里有点不清楚。这里可能有 3 种不同的情况:
- Moving through navigation controller hierarchy
- Breaking out of navigation controller hierarchy to another view controller
- Just adding another view controller to current in navigation controller stack
- 在导航控制器层次结构中移动
- 打破导航控制器层次结构到另一个视图控制器
- 只需将另一个视图控制器添加到当前导航控制器堆栈中
In first case you can use methods of UINavigationController:
在第一种情况下,您可以使用 UINavigationController 的方法:
- (UIViewController *)popViewControllerAnimated:(BOOL)animated
- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated
- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated
and use viewControllers property to navigate through the stack.
并使用 viewControllers 属性在堆栈中导航。
Ina second one, if you want to break out the hierarchy to one completely another view controller, then simply do it by:
在第二个中,如果您想将层次结构分解为一个完全不同的视图控制器,那么只需执行以下操作:
[[[UIApplication sharedApplication] keyWindow].rootViewController dismissViewControllerAnimated:YES completion:nil];
[[UIApplication sharedApplication] keyWindow].rootViewController = newController;
or even better: add second line in completion block of first line.
甚至更好:在第一行的完成块中添加第二行。
Or in third case, if you want only to make one exception, but otherwise stay in navigation controller stack, then use methods:
或者在第三种情况下,如果你只想做一个例外,但要留在导航控制器堆栈中,那么使用方法:
- (void)addChildViewController:(UIViewController *)childController
- (void)removeFromParentViewController
回答by Hermann Klecker
That depends on how you actually presented the current view controller. If it was modally, then
这取决于您实际呈现当前视图控制器的方式。如果是模态的,那么
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
If it was pushed using a navigation controller:
如果它是使用导航控制器推送的:
[self.navigationController popViewControllerAnimated:YES];