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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13987026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:35:20  来源:igfitidea点击:

iOS - Push viewController from code and storyboard

iosuiviewstoryboardxcode4.5pushviewcontroller

提问by Xose

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

谢谢

回答by Vineesh TP

Objective-C:

目标-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.

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

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

Swift:

迅速:

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

回答by Sumanth

For Storyboards, you should use performSegueWithIdentifierlike so:

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

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

回答by Alino

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

回答by footyapps27

Swift 3.x

斯威夫特 3.x

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

回答by Shashikant Kashodhan

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 创建一个标准的视图控制器。我们首先将视图控制器更改为导航控制器。选择 只需在菜单中选择“Editor”并选择“Embed in”,然后选择“Navigation Controller” Step 1. 选择 Main story board Step 2.单击 Xcode 应用程序顶部的“Editor”。第 3 步。单击“嵌入”

Xcode automatically embeds the View Controller with Navigation Controller.

Xcode 自动将视图控制器嵌入到导航控制器中。