简体   繁体   English

设置keyWindow rootViewController在iOS8中不起作用

[英]Setting the keyWindow rootViewController not working in iOS8

In my app I need the user to login and logout. 在我的应用程序中,我需要用户登录和注销。 When I want the user to logout, I delete their credentials and then: 当我希望用户注销时,我会删除他们的凭据然后:

ZSSLogin *login = [self.storyboard instantiateViewControllerWithIdentifier:@"ZSSLogin"];
[[UIApplication sharedApplication].keyWindow setRootViewController:[[UINavigationController alloc] initWithRootViewController:login]];

Which is suppose to replace the current rootViewController (tabBarController) with the login viewController . 假设用登录viewController替换当前的rootViewControllerviewController

On iOS7 this works correctly. 在iOS7上,这可以正常工作。 But, in iOS8 it will show the login VC for a split second, but then return back to the tabBarController like nothing happened. 但是,在iOS8中,它将显示登录VC一瞬间,但然后返回到tabBarController,就像没有发生任何事情一样。

Any ideas on what is going on? 关于发生了什么的任何想法?

I had the exactly same problem, login VC will show for a split second and then return back to my SplitViewController and only iOS8, iOS7 was working fine. 我有完全相同的问题,登录VC将显示一瞬间,然后返回我的SplitViewController,只有iOS8,iOS7工作正常。

On my scenario, this happened only whenever I presented an AlertView before the log out, if I went straight into changing the rootViewController without AlertView it worked fine. 在我的场景中,这只发生在我在注销之前提交AlertView时,如果我直接更改rootViewController而没有AlertView它工作正常。

So I fixed it with this simple workaround: 所以我用这个简单的解决方法修复了它:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
...

    // Added a delay to avoid an iOS8 bug, where setting rootViewControler doesn't work after dismissing an AlertView. (For some reason dispatch_after isn't working here)
    [self performSelector:@selector(logout) withObject:nil afterDelay:0.5];
...

My guess is that in iOS8, OS uses Window or rootViewController to set and remove the AlertView, stores a reference to the rootViewController and then set it back the rootViewController to that reference after dismissing the AlertView. 我的猜测是在iOS8中,OS使用Window或rootViewController来设置和删除AlertView,存储对rootViewController的引用,然后在解除AlertView后将rootViewController设置为该引用。 So if you change the rootViewController before the OS sets it back, it will replace you change. 因此,如果您在操作系统将其设置回来之前更改rootViewController,它将替换您的更改。 If you wait half a second till the OS finishes making changes to the rootViewController, the problem is solved. 如果等待半秒直到操作系统完成对rootViewController的更改,问题就解决了。 Of Course, is a supposition. 当然,这是一个假设。

I got this problem too. 我也遇到了这个问题。 This is something wrong with AlertView. 这是AlertView的错误。 You can use AlertView's another Delegate method named : alertView:didDismissWithButtonIndex: In this method call your Setting RootViewController . 您可以使用AlertView的另一个名为的Delegate方法: alertView:didDismissWithButtonIndex:在此方法中调用您的Setting RootViewController

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if(buttonIndex == 0){
        // change Application.keyWindow.rootViewController
    }
}

Actually this is an issue related to iOS 8.In iOS 8 they will keep the old view hierarchy after setting the rootViewController property of the window also.So my solution is that remove the sub views before setting the new rootviewcontroller . 实际上这是一个与iOS 8相关的问题。在iOS 8中,他们将在设置窗口的rootViewController属性后保留旧的视图层次结构。所以我的解决方案是在设置新的rootviewcontroller之前删除子视图。

Apple docs also saying, "The root view controller provides the content view of the window. Assigning a view controller to this property (either programmatically or using Interface Builder) installs the view controller's view as the content view of the window. If the window has an existing view hierarchy, the old views are removed before the new ones are installed." Apple文档还说,“根视图控制器提供窗口的内容视图。将视图控制器分配给此属性(以编程方式或使用Interface Builder)将视图控制器的视图安装为窗口的内容视图。如果窗口具有在现有的视图层次结构中,旧视图在安装新视图之前被删除。“

for (UIView * subView in self.window.rootViewController.view.subviews) {
    [subView removeFromSuperview];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM