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

时间:2012-12-21 08:59:18

标签: ios uiview storyboard xcode4.5 pushviewcontroller

我有这段代码

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

我可以更改视图,但是当我在此页面返回时,我会推送此视图,状态仍然存在。

我尝试输入此代码:

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

但没有做任何事情。

由于

5 个答案:

答案 0 :(得分:36)

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

检查2件事,

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

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

答案 1 :(得分:5)

对于Storyboard,您应该使用performSegueWithIdentifier,如下所示:

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

答案 2 :(得分:5)

使用:

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];

答案 3 :(得分:2)

Swift 3.x

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

答案 4 :(得分:0)

默认情况下,Xcode会创建一个标准视图控制器。我们首先将视图控制器更改为导航控制器。选择Simply选择菜单中的“Editor”并选择“Embed in”,然后选择“Navigation Controller” 步骤1.选择主故事板 步骤2.单击Xco​​de应用程序顶部的“编辑器”。 第3步。点击“嵌入”

Xcode自动将View Controller与导航控制器嵌入。