Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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"

Relevant code:

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)     } } 

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

like image 832
Nils Wasell Avatar asked Sep 24 '14 17:09

Nils Wasell


1 Answers

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.

like image 77
Acey Avatar answered Sep 20 '22 02:09

Acey