iOS – 从代码和故事板推送viewController

我有这个代码

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

而且我可以改变看法,但是当我在这个页面返回的时候,我会推动这个观点,这个状态依然存在。

我试着把这个代码:

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

但没有做任何事情。

谢谢

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

检查2件事情,

  1. 你的故事板标识符是正确的。

  2. 根视图有导航控制器。

对于故事板,你应该像这样使用performSegueWithIdentifier

  [self performSegueWithIdentifier:@"identifier goes here" sender:self]; 

Swift 3.x

 let viewController = storyboard?.instantiateViewController(withIdentifier: "storyboardIdentifier") as! UIViewController navigationController?.pushViewController(viewController, animated: true) 

默认情况下,Xcode创build一个标准的视图控制器。 我们首先将视图控制器更改为导航控制器。 select菜单中的简单select“编辑器”,然后select“embedded”,然后select“导航控制器”。第1步。select主要故事板第2步。单击Xco​​de应用程序顶部的“编辑器”。 第3步。点击“embedded”

Xcode自动embedded视图控制器与导航控制器。

使用:

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

要么:

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