警告:尝试在视图不在窗口层次结构中显示* on * - swift [英] Warning: Attempt to present * on * whose view is not in the window hierarchy - swift

查看:108
本文介绍了警告:尝试在视图不在窗口层次结构中显示* on * - swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果数据模型中有任何已保存的数据,我正在尝试提供 ViewController 。但是我收到以下错误:

I'm trying to present a ViewController if there is any saved data in the data model. But I get the following error:


警告:尝试显示*视图不在窗口层次结构中的* *

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

相关代码:

override func viewDidLoad() {
    super.viewDidLoad()
    loginButton.backgroundColor = UIColor.orangeColor()

    var request = NSFetchRequest(entityName: "UserData")
    request.returnsObjectsAsFaults = false

    var appDel:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
    var context:NSManagedObjectContext = appDel.managedObjectContext!

    var results:NSArray = context.executeFetchRequest(request, error: nil)!

    if(results.count <= 0){
        print("Inga resultat")
    } else {
        print("SWITCH VIEW PLOX")
        let internVC = self.storyboard?.instantiateViewControllerWithIdentifier("internVC") as internViewController
        self.presentViewController(internVC, animated: true, completion: nil)
    }
}

我尝试使用Google找到了不同的解决方案但没有成功。

I've tried different solutions found using Google without success.

推荐答案

在代码中的这一点上,视图控制器的视图只有已创建但未添加到任何视图层次结构中。如果你想尽快从那个视图控制器出现,你应该在 viewDidAppear 中做到最安全。

At this point in your code the view controller's view has only been created but not added to any view hierarchy. If you want to present from that view controller as soon as possible you should do it in viewDidAppear to be safest.

这篇关于警告:尝试在视图不在窗口层次结构中显示* on * - swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆