Nils Wasell Nils Wasell - 1 year ago 190
iOS Question

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

I'm trying to present a

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() {
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 {
let internVC = self.storyboard?.instantiateViewControllerWithIdentifier("internVC") as internViewController
self.presentViewController(internVC, animated: true, completion: nil)

Iv'e tried different solutions found using Google.


Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download