问题描述
在我的应用中,我根据用户操作(例如,登录/注销功能.
In my app I programmatically change root view controllers based on user actions e.g. login/logout functionality.
在iOS 8中-我注意到一个奇怪的问题.即使在窗口上设置了rootViewController之后,旧的层次结构仍然存在.我只是通过捕获视图层次结构对其进行了验证.
In iOS 8 - I'm noticing a strange issue. Even after setting rootViewController on the window, the old hierarchy still persists. I just verified it by capturing view hierarchy.
- (void) logout{
[self.window setRootViewController:[self loadLoginView]];
}
-(UIViewController *) loadLoginView{
WelcomeScreenVC *wsVC;
wsVC = [[WelcomeScreenVC alloc] initWithNibName:@"WelcomeScreenVC" bundle:nil];
UINavigationController *onboardingVC = [[UINavigationController alloc]initWithRootViewController:wsVC];
return onboardingVC;
}
即使执行了这一行代码之后,旧的登录视图层次结构仍然存在.如果有人可以建议幕后发生的事情,我们将不胜感激.
Even after executing this line of code, the old logged in view hierarchy still persists. Would appreciate if anybody can suggest what's happening behind the scenes.
我只是看了UIWindow setRootViewController文档,这是苹果公司要说的:
I just looked at UIWindow setRootViewController documentation and here's what Apple has to say about it:
推荐答案
我注意到了同样的事情.
I have noticed the very same thing.
基本上,我有一个相当复杂的情节提要,可以用作登录/欢迎界面.该界面位于导航控制器中,该控制器以模态形式显示另一个导航控制器.
Basically, I have a fairly complicated storyboard that acts as a login/welcome interface. This interface sits in a navigation controller, which presents another navigation controller modally.
在特定点之后,用户执行将其转换到主界面的操作.使用iOS 8视图调试器,我注意到在设置窗口的rootViewController属性后,旧的视图层次结构仍然存在.
After a certain point, the user takes an action that transitions him to the main interface. Using the iOS 8 view debugger I noticed that the old view hierarchy was still around after setting the rootViewController property of the window.
我的解决方案,现在是在重新确定window.rootViewController属性之前使用以下代码:
My solution, for now is to use the following code, right before I re-assing the window.rootViewController property:
for (UIView* subView in self.window.rootViewController.view.subviews) {
[subView removeFromSuperview];
}
[self.window.rootViewController.view removeFromSuperview];
它不是很漂亮,但是可以用.
It ain't pretty, but it works.
我注意到的另一件奇怪的事情是,使用此方法未正确清理Welcome接口以模态形式显示的viewController.我必须手动将其关闭并进行清理.
Another odd thing I noticed is that the welcome interface's modally presented viewController is not properly cleaned up using this method. I have to manually dismiss it AND do this clean up.
这篇关于UIWindow setRootViewController不清除现有层次结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!