1
votes

I try to open a second view controller in one func but this error appears:

Attempt to present <UINavigationController> on firstController whose view is not in the window hierarchy!

All solutions required :

Either put code inside viewWillAppear() or inside IBAction func

My program requires to open the second view controller from normal func

I tried all this codes:

Code1:

self.performSegueWithIdentifier("page2", sender:self)

result:

error: has no segue with identifier 'page2''

Code 2:

let secondViewController:page2 = page2()
self.presentViewController(secondViewController, animated: true,completion: nil)

result:

error: whose view is not in the window hierarchy!

Code 3:

let nextViewController = storyboard!.instantiateViewControllerWithIdentifier("next2") as! UIViewController
self.presentViewController(nextViewController, animated:true, completion:nil)

result:

fatal error: unexpectedly found nil fwom win func

Code 4:

let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("next2") as! page2
self.presentViewController(nextViewController, animated:true, completion:nil)
  dismissViewControllerAnimated(true, completion:nil)

result:

error whose view is not in the window hierarchy!

I need a solution please.

1
Segue might be your first consideration, but segue identifier is not the name of view. You need to connect your first view to the navigation view in the storyboard and give a identifier to be able to use this function.zcui93
yes i make the name of segue is page2 in the code and in the storyboard also i change the segue name to another name but it is not workematalb
Have a screenshot of your segue in the storyboard should help.zcui93
Error message means that you are trying to present view whose parent is not presented. In your case firstController has not yet been added to view hierarchy, in viewWillAppear() is called before the view controller's view is about to be added to a view hierarchy.esesmuedgars

1 Answers

0
votes

You're going to want to go with code 1, but in your storyboard, you're going to need a segue from the ViewController containing your code to the ViewController you want to display. (Ctrl+Drag from the ViewController "owner" icon to the second ViewController)

Then click on this segue, and in the inspector make sure the segue (!) gets the name "page2" - since this name is what is used to run the segue.