iOS - 从代码和故事板中推出视图控制器

15

我有这段代码

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

我可以更改视图,但我希望当我返回到这个页面时,状态能够保持不变。

我尝试放置以下代码:

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

但是并没有做任何事情。

谢谢

5个回答

41

Objective-C:

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

请注意以下几点:

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

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

Swift:

let newView = self.storyboard?.instantiateViewController(withIdentifier: "storyBoardIdentifier") as! PlaceViewController
self.navigationController?.pushViewController(newView, animated: true)

嗨,在目标视图控制器中无法获取导航。 - Jignesh B
@JigneshB:你是否连接到导航控制器?请检查第二点,2. 根视图拥有导航控制器。 - Vineesh TP

6

使用:

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

6
对于故事板,你应该像这样使用performSegueWithIdentifier:
 [self performSegueWithIdentifier:@"identifier goes here" sender:self];

在哪里?(抱歉我不知道) - Xose
你应该使用 [self performSegueWithIdentifer:@"你的标识符" sender:self]; 代替 [self.navigationController pushViewController:newView animated:YES]; - Sumanth
"No visible @interface declares the selector 'performSegueWithIdentifer:sender'" 这个错误。 - Xose
performSegueWithIdentifier是UIViewController类的一个方法。除非您的ModelController是UIViewController的子类,否则该方法将不可用。如果不完全了解您的应用程序,则无法确定。 - Sumanth
2
我建议你阅读这个教程:http://www.raywenderlich.com/5138/beginning-storyboards-in-ios-5-part-1 - Sumanth
1
我从这个类中调用这段代码:“@interface MapViewController : UIViewController {...}”,所以这个类是UIViewController的子类,对吗? - Xose

2

Swift 3.x

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

-2

默认情况下,Xcode创建一个标准的视图控制器。我们首先将视图控制器更改为导航控制器。选择菜单中的“编辑”,然后选择“嵌入”,接着选择“导航控制器”。

步骤1. 选择主故事板 步骤2. 点击Xcode应用程序顶部的“编辑”。 步骤3. 点击“嵌入”

Xcode会自动将视图控制器与导航控制器嵌入在一起。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接