Nils Wasell Nils Wasell - 1 month ago 18
iOS Question

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 datamodel. But i get the above error: "Warning: Attempt to present * on *whose view is not in the window hierarchy" when the following IF-Statement is true.

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


Iv'e tried different solutions found using Google.

Cheers.

Answer

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.