iOS - Push viewController from code and storyboard

Xose picture Xose · Dec 21, 2012 · Viewed 41.3k times · Source

I have this code

 PlaceViewController *newView = [self.storyboard instantiateViewControllerWithIdentifier:@"PlaceView"];
 [self presentViewController:newView animated:YES completion:nil];

And I can change view, but I would push this view for when I return at this page, the state persists.

I try to put this code:

[self.navigationController pushViewController:newView animated:YES];

but doesn't do anything.

Thanks

Answer

Vineesh TP picture Vineesh TP · May 28, 2014

Objective-C:

PlaceViewController *newView = [self.storyboard instantiateViewControllerWithIdentifier:@"storyBoardIdentifier"];
[self.navigationController pushViewController:newView animated:YES];

Please do notice below points,

  1. your storyboard identifier is correct.

  2. The root view have navigation Controller.

Swift:

let newView = self.storyboard?.instantiateViewController(withIdentifier: "storyBoardIdentifier") as! PlaceViewController
self.navigationController?.pushViewController(newView, animated: true)