iOS - Push viewController from code and storyboard

2019-01-09 10:54发布

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

5条回答
虎瘦雄心在
2楼-- · 2019-01-09 11:02
PlaceViewController *newView = [self.storyboard instantiateViewControllerWithIdentifier:@"storyBoardIdentifier"];
[self.navigationController pushViewController:newView animated:YES];

Check 2 things,

  1. your storyboard identifier is correct.

  2. The root view have navigation Controller.

查看更多
戒情不戒烟
3楼-- · 2019-01-09 11:03

Swift 3.x

let viewController = storyboard?.instantiateViewController(withIdentifier: "storyboardIdentifier") as! UIViewController
navigationController?.pushViewController(viewController, animated: true)
查看更多
爷的心禁止访问
4楼-- · 2019-01-09 11:11

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];
查看更多
该账号已被封号
5楼-- · 2019-01-09 11:17

For Storyboards, you should use performSegueWithIdentifier like so:

 [self performSegueWithIdentifier:@"identifier goes here" sender:self];
查看更多
Evening l夕情丶
6楼-- · 2019-01-09 11:19

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.

查看更多
登录 后发表回答