xcode 忽略 RootViewController?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10411078/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:09:46  来源:igfitidea点击:

Dismiss to RootViewController?

iphoneiosxcode

提问by Bashud

I have an RegistrationViewController and a LoginViewController:

我有一个 RegistrationViewController 和一个 LoginViewController:

LoginViewController is my InitialViewController/RootViewController.

LoginViewController 是我的 InitialViewController/RootViewController。

If i registrate and click on the Registrate-Button, it will push automatically to the MainViewController. If i push the Logout-Button, it dismissed to the RegistrationViewController, this is because i use [self dismissModalViewController animated:YES].

如果我注册并单击注册按钮,它将自动推送到 MainViewController。如果我按下注销按钮,它会关闭到 RegistrationViewController,这是因为我使用了 [self deniedModalViewController 动画:YES]。

  - (IBAction)logoutPressed:(id)sender {
  [self dismissModalViewControllerAnimated:YES];
   }

How can i Dismiss to the LoginViewController if i push the Logout-Button.

如果我按下注销按钮,我如何关闭 LoginViewController。

回答by Heisenbean

-(void)dismissToRootViewController
{
    UIViewController *vc = self;
    while (vc.presentingViewController) {
        vc = vc.presentingViewController;
    }
    [vc dismissViewControllerAnimated:YES completion:nil];
}

回答by Caleb

You can use the UINavigationController method -popToRootViewControllerAnimated:.

您可以使用 UINavigationController 方法-popToRootViewControllerAnimated:

- (IBAction)logoutPressed:(id)sender {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

If you're instead talking about several modal views being presented one on top of another, you can dismiss all of them by sending -dismissViewControllerAnimated:completion:or the older -dismissModalViewControllerAnimated:to the lowest one in the stack, as described in the documentation:

如果您正在谈论多个模态视图,一个在另一个顶部呈现,您可以通过发送-dismissViewControllerAnimated:completion:或更旧的-dismissModalViewControllerAnimated:到堆栈中最低的一个来消除所有它们,如文档中所述:

If you present several view controllers in succession, thus building a stack of presented view controllers, calling this method on a view controller lower in the stack dismisses its immediate child view controller and all view controllers above that child on the stack. When this happens, only the top-most view is dismissed in an animated fashion

如果您连续呈现多个视图控制器,从而构建呈现的视图控制器堆栈,则在堆栈中较低的视图控制器上调用此方法会解除其直接子视图控制器以及堆栈中该子视图之上的所有视图控制器。发生这种情况时,只有最顶层的视图以动画方式消失

回答by Kuldeep Saxena

Try it, worked for me.

试试吧,对我有用。

 [[[UIApplication sharedApplication] keyWindow].rootViewController dismissViewControllerAnimated:YES completion:nil];

回答by Karthik damodara

If you are using stack of View Controllers using UINavigationController, It is wise to use following methods.

如果您使用 UINavigationController 使用视图控制器堆栈,则使用以下方法是明智的。

1. Pop the current top ViewController use
 [[self navigationController] popViewControllerAnimated:YES];

2. Pop to RootViewController use
[[self navigationController] popToRootViewControllerAnimated:YES];

3. For that matter popping to any ViewController use
 [[self navigationController] popToViewController:viewController animated:YES];