Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - Push viewController from code and storyboard

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

like image 204
Xose Avatar asked Dec 21 '12 08:12

Xose


People also ask

How do I push a presented ViewController?

VC3 -> present VC2 -> VC1 VC2 needs to be in a UINavigationController then after you present it you can push VC3. The back button will work as expected on VC3, for VC2 you should call dismiss when the back button is pressed. Try implementing some of that in code and then update your question.

How do I add a ViewController to a storyboard?

To create a new view controller, select File->New->File and select a Cocoa Touch Class. Choose whether to create it with Swift or Objective-C and inherit from UIViewController . Don't create it with a xib (a separate Interface Builder file), as you will most likely add it to an existing storyboard.


1 Answers

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)
like image 81
Vineesh TP Avatar answered Oct 19 '22 02:10

Vineesh TP