ios 从另一个 ViewController 呈现故事板 ViewController

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8924052/
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 16:23:57  来源:igfitidea点击:

Present storyboard ViewController from another ViewController

iosios5uiviewcontrolleruistoryboard

提问by Ranjit

I have multiple UIViewControllers in my iOS App's Storyboard. I want to open one of the UIViewControllers (in the storyboard) from a different UIViewController.

我的 iOS 应用程序的故事板中有多个 UIViewControllers。我想从不同的 UIViewController 打开一个 UIViewControllers(在故事板中)。

I've tried the code below, but it isn't working even though I used it before iOS 5 and it worked fine.

我已经尝试过下面的代码,但它不起作用,即使我在 iOS 5 之前使用过它并且运行良好。

- (IBAction)addNotes:(id)sender {
    NotesViewController *notesView = [[NotesViewController alloc] initWithNibName:@"NotesViewController" bundle:nil];
    [self presentModalViewController:notesView animated:YES];
}

How can I perform this action with iOS 5 Storyboards?

如何使用 iOS 5 Storyboards 执行此操作?

回答by DAS

Assuming you have storyboard, go to storyboard and give your VC an identifier (inspector), then do:

假设您有故事板,请转到故事板并为您的 VC 提供一个标识符(检查器),然后执行以下操作:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"IDENTIFIER"];
[self.navigationController pushViewController:vc animated:YES];

Assuming you have a xib file you want to do:

假设您有一个想要执行的 xib 文件:

UIViewController *vc = [[UIViewController alloc] initWithNibName:@"NIBNAME" bundle:nil];
[self.navigationController pushViewController:vc animated:YES];

Without a xib file:

没有xib文件:

UIViewController *vc = [[UIViewController alloc] init];
[self.navigationController pushViewController:vc animated:YES];

回答by Vakas

Following will work on Swift 3.0and above.

以下将适用于Swift 3.0及更高版本。

StoryBoard

故事板

let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let mainViewController = storyBoard.instantiateViewController(withIdentifier: "Identifier")
self.navigationController?.pushViewController(mainViewController, animated: true)

.xib

.xib

    let viewController = UIViewController(nibName: "NibName", bundle: nil)
    self.navigationController?.pushViewController(viewController, animated: true)

Without .xib

没有 .xib

let viewController = UIViewController()
self.navigationController?.pushViewController(viewController, animated: true)

回答by Tuan Huynh

Update with the Swift 3.1 version

使用 Swift 3.1 版本更新

if you haven'tembed navigation controller then

如果你还没有嵌入导航控制器那么

let storyBoard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let viewController2 = storyBoard.instantiateViewController(withIdentifier: "ViewController2")
self.present(viewController2, animated: true, completion: nil)

and, if you hadembed navigation controller then

而且,如果你已经嵌入导航控制器,然后

let storyBoard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let viewController2 = storyBoard.instantiateViewController(withIdentifier: "ViewController2")
self.navigationController?.pushViewController(viewController2, animated: true)

回答by Shahzaib Maqbool

In swift 4.2 answer for those who want this answer without navigation in modally and in swift updated versions.

在 swift 4.2 中回答那些想要这个答案而无需在模态和快速更新版本中导航的人。

let storyboard = UIStoryboard(name: "YourStoryboardName", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "mainViewController")
self.present(controller, animated: true, completion: nil)

回答by Franck Ndame

Swift 5

斯威夫特 5

let vc = self.storyboard!.instantiateViewController(withIdentifier: "ControllerIdentifier")
self.present(vc, animated: true, completion: nil)

回答by Rahul Ramachandra

UIViewController *initialHelpView = [[UIStoryboard storyboardWithName:@"StoryBoard_IDentifier" bundle:nil] instantiateViewControllerWithIdentifier:@"ViewController_Identifier"];
[self presentViewController:initialHelpView animated:YES completion:nil];