Warning: Attempt to present A on B whose view is not in the window hierarchy!
来源:互联网 发布:西安朝阳软件 编辑:程序博客网 时间:2024/05/17 02:48
Warning: Attempt to present <UINavigationController: 0x8d514e0> on <OAuthViewController: 0xa044a60> whose view is not in the window hierarchy!
其功能是OAuthViewController用于用户授权,在获取用户的授权后将拿到的access_token通过NSUserDefaults保存起来。那么在下一次打开程序时,首先判断access_token是否已经存在,如果已经存在就直接跳转到UINavigationController。代码如下:
- (void)viewDidLoad { [super viewDidLoad]; spinner_view.hidesWhenStopped = YES; [webView setDelegate:self]; [webView setScalesPageToFit:YES]; // 如果已经保存了授权用户的access_token,那么直接跳转到UINavigationController NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; access_token = [userDefaults objectForKey:@"access_token"]; if (access_token) { [self performSegueWithIdentifier:@"gotoSay" sender:nil]; return; } NSString *paramClientID = [NSString stringWithFormat:@"client_id=%@", API_KEY]; NSString *paramRedirect_uri = [NSString stringWithFormat:@"redirect_uri=%@", REDIRECT_URI]; NSString *paramResponse_type = @"response_type=code"; NSString *paramScope = @"shuo_basic_r,shuo_basic_w,douban_basic_common"; // 在其它API调用时可能要扩展该作用域参数 NSString *getAuthCode = [NSString stringWithFormat:@"%@?%@&%@&%@&%@", GET_AUTHORIZATION_CODE_URL, paramClientID, paramRedirect_uri, paramResponse_type, paramScope]; NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:getAuthCode]]; [spinner_view startAnimating]; [webView loadRequest:request];}
问题出现了,无法完成视图的跳转,但是用户的access_token已经在preferences的plist文件中成功保存了。控制台输出的错误信息如上所示。
查了一下资料,发现了问题所在(StackOverflow上面的高手真的很多啊),先给出两个对我很有帮助的网址:
[ios开发异常]whose view is not in the window hierarchy!
loadView、viewDidLoad、viewWillAppear、viewDidAppear等详解
解决方法:
将验证access_token是否已经存在的代码转移到viewDidAppear方法中:
- (void)viewDidAppear:(BOOL)animated { // 如果已经保存了授权用户的access_token,那么直接跳转到UINavigationController NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; access_token = [userDefaults objectForKey:@"access_token"]; if (access_token) { [self performSegueWithIdentifier:@"gotoSay" sender:nil]; return; }}
分析:
在进行单步调试时,如果你细心点就会发现,只有在viewDidLoad方法完全执行完毕,才会真正在window中加载对应的界面。这样一来,在修改前的程序中就有问题出现了,注意该动作发生在A的viewDidLoad方法中:
[self performSegueWithIdentifier:@"gotoSay" sender:nil];
作用是从A跳转到B中。
那么在segue调用后,程序将调用B的viewDidLoad方法,在B的viewDidLoad方法执行完后,又回到了A的viewDidLoad的方法中执行剩下的语句。那么在window加载视图时其层次结构便发生了混乱:window加载完B的view后又回到了A并加载了A的view(所以可以看到在程序中加载的是A的view),这时window的目录结构下找不到B的view,于是报错。
viewDidAppear方法和viewDidLoad方法的区别在于:viewDidLoad方法调用时视图还没完全过渡到window中,viewDidAppear方法调用时,视图已经完全过渡到window中了。
所以在viewDidAppear方法中调用performSegue方法实现视图跳转就不会出现以上的问题了,因为程序将有序地先执行A的viewDidLoad方法,在该方法完结后,再在viewDidAppear方法中执行segue跳转并执行B的viewDidLoad方法,这样就不会发生混乱了。
- Warning: Attempt to present A on B whose view is not in the window hierarchy!
- Warning: Attempt to present A on B whose view is not in the window hierarchy!
- Warning: Attempt to present A on B whose view is not in the window hierarchy!
- 对Warning: Attempt to present on whose view is not in the window hierarchy!的解决方案
- 对Warning: Attempt to present on whose view is not in the window hierarchy!的解决方案
- 对Warning: Attempt to present on whose view is not in the window hierarchy!的解决方案
- Warning: Attempt to present * on * whose view is not in the window hierarchy!
- Warning: Attempt to present xx on yy whose view is not in the window hierarchy!
- 对Warning: Attempt to present on whose view is not in the window hierarchy!的解决方案
- 对Warning: Attempt to present on whose view is not in the window hierarchy!的解决方案
- xcode编译Warning: Attempt to present xx on yy whose view is not in the window hierarchy!
- iOS - 报错 Warning: Attempt to present <xx: xx> on <xx: xx> whose view is not in the window hierarchy!
- Warning: Attempt to present ControllerA and ControllerB whose view is not in the window hierarchy!
- Attempt to present on whose view is not in the window hierarchy!
- Attempt to present on whose view is not in the window hierarchy!
- iOS 简单的实现手机号正误的判断(Warning: Attempt to present... whose view is not in the window hierarchy!
- 处理iOS报错: Warning “attempt to present ViewController whose view is not in the window hierarchy”
- Attempt to present VC whose view is not in the window hierarchy 解决
- 段页式存储管理
- LINUX C语言文件的读写(非二进制)
- poj 3419 Difference Is Beautiful (区间最长连续不重复数 dp+二分+RMQ)
- windows bat脚本编写
- 使用Fragment实现简单的数据交互
- Warning: Attempt to present A on B whose view is not in the window hierarchy!
- Polay定理总结
- order by 、group by 、having的用法区别
- 动画效果之360桌面清除动画(一)
- Android_Fragment_Fragment详解
- Java多线程之CyclicBarrier(一)
- Tomcat从零开始(十六)-----tomcat安全
- 排序算法_堆排序
- A+B Problem