iOS开发出错whose view is not in the window hierarchy!的解决
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处.
如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;)
一个简单的单窗口App在运行时出现错误:
2016-04-07 14:28:48.411 BlurViewAndPopView[4364:168520] Warning: Attempt to present <UIAlertController: 0x7a0a4e00> on <BlurViewAndPopView.ViewController: 0x797757d0> whose view is not in the window hierarchy!
2016-04-07 14:28:48.935 BlurViewAndPopView[4364:168520] Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior (<UIAlertController: 0x7a0a4e00>)
该app的功能是在root VC中弹出一个popover视图,其中包含一个表视图,在点击表视图中的某一行时回调root VC中的闭包,完成改行内容的对话框弹出效果.
分析如下:根据错误信息,可以清楚看到在试图弹出对话框时,root VC不在窗口的继承体系中,这意味着此时root VC不在window中.
查看popover的cellSelect回调方法:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let selectedItem = items[indexPath]
selectionHandler?(selectedItem: selectedItem)
dismissViewControllerAnimated(true, completion: nil)
}
可以看到在调用root VC注册的selectionHandler闭包之后才做的dismissVC的操作,这显然顺序不对!
我们可以这样修改:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let selectedItem = items[indexPath]
defer {
selectionHandler?(selectedItem: selectedItem)
}
dismissViewControllerAnimated(true, completion: nil)
}
好了!现在将selectionHandler的回调放到final中,即可保证在root VC中的操作是在dismissVC之后才开始的,这是root VC应该在window的继承体系中了.
iOS开发出错whose view is not in the window hierarchy!的解决的更多相关文章
- IOS 开发中 Whose view is not in the window hierarchy 错误的解决办法
在 IOS 开发当中经常碰到 whose view is not in the window hierarchy 的错误,该错误简单的说,是由于 "ViewController" ...
- swift一次 Attempt to present on whose view is not in the window hierarchy的解决方法
做的是二维码扫描,扫描后识别为URL的话就跳转到webview 加载网页,用的是代理传值的方式.扫描到了 值传递到主页 扫描窗体退出,检测值是否是http://开头 是网页就跳转. 问题出在传值到主界 ...
- 详解iOS开发之自定义View
iOS开发之自定义View是本文要将介绍的内容,iOS SDK中的View是UIView,我们可以很方便的自定义一个View.创建一个 Window-based Application程序,在其中添加 ...
- Warning: Attempt to present A on B whose view is not in the window hierarchy!
昨天写豆瓣发广播Demo的时候,为了写Demo的简单,就使用了Storyboard,结果执行视图跳转时遇到了这个问题: Warning: Attempt to present <UINaviga ...
- whose view is not in the window hierarchy
参考:http://www.jianshu.com/p/9e90cb866fdf 在做界面跳转的时候,我们经常会用到这两个函数 func dismissViewControllerAnimated(f ...
- Warning: Attempt to present on whose view is not in the window hierarchy!
当我想从一个VC跳转到另一个VC的时候,一般会用 - (void)presentViewController:(UIViewController *)viewControllerToPresent a ...
- Swift - whose view is not in the window hierarchy 问题解决方法
问题现象:想在页面初始化的时候,使用self.presentViewController方法弹出个告警提示框UIAlertController.但行后报了个如下告警,同时告警框也出不来. 1 2015 ...
- 错误:Warning: Attempt to present <UIAlertController: 0x7fd192806e20> on <ViewController: 0x7fd1928048d0> whose view is not in the window hierarchy!
系统:mac OS 10.12 (16A323) Xcod:8.3.3 错误:Warning: Attempt to present <UIAlertController: 0x7fd1928 ...
- Attempt to present <TestViewController2: 0x7fd7f8d10f30> on <ViewController: 0x7fd7f8c054c0> whose view is not in the window hierarchy!
当 storyboard里面的 按钮 即连接了 类文件里面的点击方法 又 连接了 storyboard里 另一个 控制器的 modal 就会出现类似Attempt to present & ...
随机推荐
- ●洛谷P3348 [ZJOI2016]大森林
题链: https://www.luogu.org/problemnew/show/P3348 题解: LCT,神题 首先有这么一个结论: 每次的1操作(改变生长点操作),一定只会会对连续的一段区间产 ...
- NOIP2016 玩脱记
NOIP前: NOIP前停课了一个多月,这一个多月里浪得飞起,内心十分紧张,然后就不知不觉就到NOIP了. Day 0: 上火车前ryc给我们出了道题"一个数列,只有两个数出现了奇数次,找出 ...
- python中如何将生成等差数列和等比数列
在python库numpy 中提供了函数linspace和logspace函数用于生产等差数列和等比数列. 1.linspace函数生成等差数列 def linspace(start, sto ...
- Yii2.0源码阅读-behavior的实现原理
Yii2.0中的一个思想就是组件化的思想,所以.大多数的类都直接或间接的继承自yii\base\Component,而组件的三大功能:属性.事件.行为. 行为的目的是为了方便的扩展一个类的功能,而不需 ...
- URI与URL
为了区分URI与URL,我们要引入URN URI = Universal Resource Identifier 统一资源标志符URL = Universal Resource Locator 统一资 ...
- 最新版-MySQL8.0 安装 - 改密码 之坑
1. 需求背景 最近需要在一台性能一般的电脑上使用数据库,所以决定安装MySQL数据库,以前安装都是使用WorkBench自动化安装,但安装过程太慢占用空间过大,于是下载zip压缩包.之所以选择选择M ...
- python--ftp服务器(pyftpdlib)
# -*- coding: utf-8 -*-# @Time : 2018/4/11 16:47# @Author : liuxiaobing# @File : test2.py# @Software ...
- python学习之路前端-Dom
Dom简介 文档对象模型(Document Object Model,DOM)是一种用于HTML和XML文档的编程接口.它给文档提供了一种结构化的表示方法,可以改变文档的内容和呈现方式.我们最为 ...
- map函数、filer函数、reduce函数的用法和区别
Map函数 map函数的用法如下: def add_one(x): return x+1 #使用普通函数 v1 = map(add_one,[1,2,3]) v1 = list(v1) print(v ...
- C++笔记007:易犯错误模型——类中为什么需要成员函数
先看源码,在VS2010环境下无法编译通过,在VS2013环境下可以编译通过,并且可以运行,只是运行结果并不是我们期待的结果. 最初通过MyCircle类定义对象c1时,为对象分配内存空间,r没有初始 ...