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

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


1

2015-03-10 09:55:34.197 Test[1140:29622] Warning: Attempt to present <UIAlertController: 0x7c95ca20> on <Test.ViewController: 0x7a6afc60> whose view is not in the window hierarchy!

解决办法:原来的调用代码是写在viewDidLoad方法中,这个表示视图加载完毕。我们应该把方法调用放到viewDidApper中,这个是UIViewController对象的视图已经加入到窗口时调用。


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

import UIKit

class ViewController: UIViewController ,UIActionSheetDelegate {

    override func viewDidLoad() {

        super.viewDidLoad()

    }

    

    override func viewDidAppear(animated: Bool){

        super.viewDidAppear(animated)

        

        var alertController = UIAlertController(title: "系统提示",

            message: "您确定要离开hangge.com吗?", preferredStyle: UIAlertControllerStyle.Alert)

        var cancelAction = UIAlertAction(title: "取消", style: UIAlertActionStyle.Cancel, handler: nil)

        var okAction = UIAlertAction(title: "好的", style: UIAlertActionStyle.Default, handler: nil)

        alertController.addAction(cancelAction)

        alertController.addAction(okAction)

        self.presentViewController(alertController, animated: true, completion: nil)

    }

      

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

}

时间: 2024-11-06 13:54:42

Swift - whose view is not in the window hierarchy 问题解决方法的相关文章

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

当我想从一个VC跳转到另一个VC的时候,一般会用 - (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^)(void))completion;当然也可以用导航push. 有时会遇到了此类警告:Warning: Attempt to present on whose view is not in the window hier

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

在 IOS 开发当中经常碰到 whose view is not in the window hierarchy 的错误,该错误简单的说,是由于 "ViewController" 还没有被夹在,就调用该 ViewController 或者 ViewController 内的方法时,就会报这个错误. 在不同地方调用 ViewController,解决的方法也不太一样. 1. 在 一个 ViewController 里面调用另外一个 ViewController 是出现这个错误: 该错误一

whose view is not in the window hierarchy

参考:http://www.jianshu.com/p/9e90cb866fdf 在做界面跳转的时候,我们经常会用到这两个函数 func dismissViewControllerAnimated(flag:Bool, completion: (() ->Void)?) func presentViewController(viewControllerToPresent:UIViewController, animated flag:Bool, completion: (() ->Void)?

Attempt to present on whose view is not in the window hierarchy!

在开发的时候,有时候会遇到 1.ios attempt to present whose view is not in the window hierarchy 2.Warning: Attempt to present on whose view is not in the window hierarchy! 等等这样类似的提示,只要里面提示有 window hierarchy,都是view hierarchy的理解不到位导致的. 上面的问题都是在一个controller的view还没加到wi

对Warning: Attempt to present XXX on XXX whose view is not in the window hierarchy!的解决方案

下面是我找到的最靠谱的答案了.尝试后问题果然解决了.分享给大家.http://blog.csdn.net/sbvfhp/article/details/19826221 当我想从一个VC跳转到另一个VC的时候, 一般会用- (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^)(void))completion;当然也可以用导

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

UIViewController 显示的主页面 添加了一个scrollView UICollectionViewController 自带collectionView UITableViewController A自带 tableView UITableViewController  B 被present的界面 把collectionView,tableView 添加到scrollView上以显示滑动效果 问题,从UICollectionViewController 的cell选中present

whose view is not in the window hierarchy! 解决办法

self.view.addSubview(alert.view) self.addChildViewController(alert)

Warning: Attempt to present &lt;UIAlertController: 0x7fb6ab704950&gt; on &lt;UIViewController: 0x7fb6ab51c810&gt; whose view is not in the window hierarchy!

对于在UIView上,是不能跳转的,所以我们通常要借助UIViewController,但经常会出现UIViewController没被加载的情况.这时就会报这个错. 解决方法: viewCon = (UIApplication.shared.keyWindow?.rootViewController)!  self.viewCon.present(alertView, animated: true, completion: nil); 希望后面的童鞋不要掉坑里了哦!!!!

debug debug Unable to add window android.view.View--permission denied for this window type

1 AlertDialog builder=new AlertDialog.Builder(context).setTitle("注意:") 2 .setIcon(R.drawable.dailog_icon) 3 .setMessage("MS0"+ReceiveNumber+"请求建立语音连接,是否同意?") 4 .setPositiveButton("同意", new DialogInterface.OnClickLis