ios 使用导航控制器在 Storyboard 中显示视图控制器 - Swift
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25326647/
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
Present View Controller in Storyboard with a Navigation Controller - Swift
提问by Ryan
I am currently showing a viewController in my new storyboard below:
我目前在下面的新故事板中显示一个 viewController:
var storyboard : UIStoryboard = UIStoryboard(name: AccountStoryboard, bundle: nil)
var vc : WelcomeViewController = storyboard.instantiateViewControllerWithIdentifier("WelcomeID") as WelcomeViewController
vc.teststring = "hello"
self.presentViewController(vc, animated: true, completion: nil)
However, this presents the viewcontroller without its embedded navigation controller. I tried changing "WelcomeID" to the navigation controller within the storyboard - however to no success.
然而,这呈现了没有嵌入式导航控制器的视图控制器。我尝试将“WelcomeID”更改为故事板中的导航控制器 - 但是没有成功。
I got this working in Objective -C, however don't know how to transform into swift:
我在 Objective -C 中得到了这个,但是不知道如何转换为 swift:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"SetupStoryboard" bundle:nil];
UINavigationController *navigationController1 = [storyboard instantiateInitialViewController];
navigationController1.modalPresentationStyle = UIModalPresentationFormSheet;
navigationController1.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
WelcomeViewController *vc = (WelcomeViewController *)navigationController1.viewControllers[0];
vc.teststring = @"Hello";
[self presentViewController:navigationController1 animated:YES completion:nil];
How can you do this in swift?
你怎么能快速做到这一点?
回答by Chris Wagner
You're definitely on the right track. Unfortunately when you reference a view controller by its storyboard ID it will ignore the fact it is embedded within anything. Same goes for segues when you segue to something embedded, the destination view controller will be the embedding controller, not the controller you're usually interested in. Anyhow, you should be able to fix the problem in a similar way you've done in Objective-C, so this is just an exercise in syntax porting.
你绝对是在正确的轨道上。不幸的是,当您通过故事板 ID 引用视图控制器时,它会忽略它嵌入在任何内容中的事实。当您转入嵌入的东西时,转场也是如此,目标视图控制器将是嵌入控制器,而不是您通常感兴趣的控制器。无论如何,您应该能够以类似的方式解决问题Objective-C,所以这只是语法移植的练习。
Edit: Define storyboard name with string now
编辑:现在用字符串定义故事板名称
let storyboard : UIStoryboard = UIStoryboard(name: "AccountStoryboard", bundle: nil)
let vc : WelcomeViewController = storyboard.instantiateViewControllerWithIdentifier("WelcomeID") as WelcomeViewController
vc.teststring = "hello"
let navigationController = UINavigationController(rootViewController: vc)
self.presentViewController(navigationController, animated: true, completion: nil)
OR you can give your embedding view controller an ID and instantiate that instead.
或者你可以给你的嵌入视图控制器一个 ID 并实例化它。
回答by abdul sathar
let secondViewController = self.storyboard?.instantiateViewControllerWithIdentifier("WelcomeID") as SecondViewController
self.navigationController?.pushViewController(secondViewController, animated: true)
Class Name is : SecondCiewController
类名是:SecondCiewController
回答by Harjot Singh
The answer given by @Chris is works good in older version of swift.
@Chris 给出的答案在旧版本的 swift 中效果很好。
Update Swift 3 & Swift 4
更新 Swift 3 和 Swift 4
let storyboard : UIStoryboard = UIStoryboard(name: "AccountStoryboard", bundle: nil)
let vc : WelcomeViewController = storyboard.instantiateViewController(withIdentifier: "WelcomeID") as! WelcomeViewController
vc.teststring = "hello"
let navigationController = UINavigationController(rootViewController: vc)
self.present(navigationController, animated: true, completion: nil)
Thanks!!!
谢谢!!!
回答by Massimo Pavanel
Remember to setup the modalPresentationStyle property, in programmatically present the IB settings are ignored.
请记住设置 modalPresentationStyle 属性,在以编程方式呈现时,IB 设置将被忽略。
let sb = UIStoryboard(name: "retail_carrello", bundle: nil)
guard let carrelloVC = sb.instantiateViewController(withIdentifier: "mainCarrello") as? retail_carrello else { return }
let navController = UINavigationController(rootViewController: carrelloVC)
navController.modalPresentationStyle = .fullScreen //<--- remember to set up this if you need fullscreen
present(navController, animated: true, completion: nil)
回答by Aditya A.Rajan
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "SalesVC") as! SalesVC
navigationController?.pushViewController(vc, animated: true)