大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处.

如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;)


一个简单的单窗口App在运行时出现错误:

  1. 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!
  2. 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回调方法:

  1. override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
  2. let selectedItem = items[indexPath]
  3. selectionHandler?(selectedItem: selectedItem)
  4. dismissViewControllerAnimated(true, completion: nil)
  5. }

可以看到在调用root VC注册的selectionHandler闭包之后才做的dismissVC的操作,这显然顺序不对!

我们可以这样修改:

  1. override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
  2. let selectedItem = items[indexPath]
  3. defer {
  4. selectionHandler?(selectedItem: selectedItem)
  5. }
  6. dismissViewControllerAnimated(true, completion: nil)
  7. }

好了!现在将selectionHandler的回调放到final中,即可保证在root VC中的操作是在dismissVC之后才开始的,这是root VC应该在window的继承体系中了.

iOS开发出错whose view is not in the window hierarchy!的解决的更多相关文章

  1. IOS 开发中 Whose view is not in the window hierarchy 错误的解决办法

    在 IOS 开发当中经常碰到 whose view is not in the window hierarchy 的错误,该错误简单的说,是由于 "ViewController" ...

  2. swift一次 Attempt to present on whose view is not in the window hierarchy的解决方法

    做的是二维码扫描,扫描后识别为URL的话就跳转到webview 加载网页,用的是代理传值的方式.扫描到了 值传递到主页 扫描窗体退出,检测值是否是http://开头 是网页就跳转. 问题出在传值到主界 ...

  3. 详解iOS开发之自定义View

    iOS开发之自定义View是本文要将介绍的内容,iOS SDK中的View是UIView,我们可以很方便的自定义一个View.创建一个 Window-based Application程序,在其中添加 ...

  4. Warning: Attempt to present A on B whose view is not in the window hierarchy!

    昨天写豆瓣发广播Demo的时候,为了写Demo的简单,就使用了Storyboard,结果执行视图跳转时遇到了这个问题: Warning: Attempt to present <UINaviga ...

  5. whose view is not in the window hierarchy

    参考:http://www.jianshu.com/p/9e90cb866fdf 在做界面跳转的时候,我们经常会用到这两个函数 func dismissViewControllerAnimated(f ...

  6. Warning: Attempt to present on whose view is not in the window hierarchy!

    当我想从一个VC跳转到另一个VC的时候,一般会用 - (void)presentViewController:(UIViewController *)viewControllerToPresent a ...

  7. Swift - whose view is not in the window hierarchy 问题解决方法

    问题现象:想在页面初始化的时候,使用self.presentViewController方法弹出个告警提示框UIAlertController.但行后报了个如下告警,同时告警框也出不来. 1 2015 ...

  8. 错误: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 ...

  9. Attempt to present <TestViewController2: 0x7fd7f8d10f30> on <ViewController: 0x7fd7f8c054c0> whose view is not in the window hierarchy!

    当 storyboard里面的 按钮 即连接了 类文件里面的点击方法  又  连接了   storyboard里 另一个  控制器的  modal 就会出现类似Attempt to present & ...

随机推荐

  1. 2-XOR-SAT

    [题目描述]SAT(Satisfiability,可满足性)问题是著名的 NP 完全问题,它的内容是:判断由有限个布尔变量及其“非”用“或”操作连接起来的表达式组是否可以都为 TRUE.2-SAT 问 ...

  2. 【BZOJ1059】【ZJOI2007】矩阵游戏

    Description 小Q是一个非常聪明的孩子,除了国际象棋,他还很喜欢玩一个电脑益智游戏――矩阵游戏.矩阵游戏在一个N*N黑白方阵进行(如同国际象棋一般,只是颜色是随意的).每次可以对该矩阵进行两 ...

  3. 【IOI1998】Picture(扫描线+线段树)

    问题来源:IOI1998 D2T1 题意:就是在一个平面内给出n个矩形,叫你计算将这些矩形合并以后,新图形的周长. 例如: 上图是原本的矩形们 ---------->合并后的图形 解题思路:拿一 ...

  4. solr6.6初探之配置篇

    一.solr的简介 1) solr是企业级应用的全文检索项目,它是基于Apache Lucence搜索引擎开发出来的用于搜索的应用工程 2) solr最新版本6.6 下载地址:下载地址 二 启动与配置 ...

  5. 第四次C语言作业

    (一)改错题 输出三角形的面积和周长,输入三角形的三条边a.b.c,如果能构成一个三角形,输出面积area和周长perimeter(保留2位小数):否则,输出"These sides do ...

  6. SQL之DISTINCT

    警告:不能部分使用DISTINCT. DISTINCT关键字作用于所有的列,不仅仅是跟在其后的那一列.例如,你指定SELECT DISTINCT vend_id, prod_price,除非指定的两列 ...

  7. Linux sort -n 与 -g 排序对比

    公司业务需要,天天用awk统计数值然后排序,出问题了,如下: count.sh 是一个统计脚本,把awk输出的值按占比.条数.类型 在重新输出 awk -F\| '{print $16}' *MQTT ...

  8. AngularJs开发——指令与控制器间的通信

    (原文:http://www.html5jscss.com/controller-between-directive.html) 指令与控制器之间通信,跟控制器间的通信.指令间通信也类似,也是下几种方 ...

  9. struts2 Action获取表单传值(属性,类))

    http://blog.csdn.net/sd0902/article/details/8393157 求大神告知两种方法的不同点 都是写个set方法就行了

  10. JAVA (集合和数据结构)

    Collection和Collections的区别: 1.java.util.Collection 是一个集合接口.它提供了对集合对象进行基本操作的通用接口方法.Collection接口在Java 类 ...