15

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

NANNAV
  • 4,875
  • 4
  • 32
  • 50
Xose
  • 544
  • 1
  • 4
  • 12

5 Answers5

41

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)
Vineesh TP
  • 7,755
  • 12
  • 66
  • 130
6

For Storyboards, you should use performSegueWithIdentifier like so:

 [self performSegueWithIdentifier:@"identifier goes here" sender:self];
Buneme Kyakilika
  • 1,202
  • 3
  • 13
  • 34
Sumanth
  • 4,913
  • 1
  • 24
  • 39
  • Where? (Sorry for not knowing) – Xose Dec 21 '12 at 09:05
  • Instead of `[self.navigationController pushViewController:newView animated:YES];` you should use `[self performSegueWithIdentifer:@"You identifier" sender:self];` – Sumanth Dec 21 '12 at 09:06
  • "No visible @interface declares the selector 'performSegueWithIdentifer:sender'" I have this error – Xose Dec 21 '12 at 09:09
  • performSegueWithIdentifier is a method of the UIViewController class. Unless your ModelController is a sub class of UIViewController the method will not be available. Without understanding your application fully it's not possible to be certain – Sumanth Dec 21 '12 at 09:14
  • 2
    i suggest you to read http://www.raywenderlich.com/5138/beginning-storyboards-in-ios-5-part-1 this tutorial – Sumanth Dec 21 '12 at 09:18
  • 1
    I call this code from this class: "@interface MapViewController : UIViewController {...}" so, this class is a subclass of UIViewController right? – Xose Dec 21 '12 at 09:31
6

Use:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"STORYBOARD_NAME" bundle:nil];
PlaceViewController *newView = [storyboard instantiateViewControllerWithIdentifier:@"PlaceView"];
[self presentViewController:newView animated:YES completion:nil];

Or:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"STORYBOARD_NAME" bundle:nil];
PlaceViewController *newView = [storyboard instantiateViewControllerWithIdentifier:@"PlaceView"];
[self.navigationController pushViewController:newView animated:YES];
Alino
  • 153
  • 1
  • 9
2

Swift 3.x

let viewController = storyboard?.instantiateViewController(withIdentifier: "storyboardIdentifier") as! UIViewController
navigationController?.pushViewController(viewController, animated: true)
footyapps27
  • 3,982
  • 2
  • 25
  • 42
-2

By default, Xcode creates a standard view controller. we first change the view controller to navigation controller. Select the Simply select “Editor” in the menu and select “Embed in”, followed by “Navigation Controller” Step 1. Select Main story board Step 2.Click on "Editor" on top of your Xcode application. Step 3. Click on "Embed In"

Xcode automatically embeds the View Controller with Navigation Controller.