我目前在下面的新故事板中展示了一个 viewController:
I am currently showing a viewController in my new storyboard below:
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)
但是,这会呈现没有嵌入式导航控制器的视图控制器.我尝试将WelcomeID"更改为情节提要中的导航控制器 - 但没有成功.
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.
我在 Objective-C 中得到了这个工作,但是不知道如何转换成 swift:
I got this working in Objective -C, however don't know how to transform into 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?
你绝对是在正确的轨道上.不幸的是,当您通过情节提要 ID 引用视图控制器时,它将忽略它嵌入任何内容的事实.当您选择嵌入的东西时,segues 也是如此,目标视图控制器将是嵌入控制器,而不是您通常感兴趣的控制器.无论如何,您应该能够以类似的方式解决问题Objective-C,所以这只是一个语法移植练习.
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.
现在用字符串定义故事板名称
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)
或者你可以给你的嵌入视图控制器一个 ID 并实例化它.
OR you can give your embedding view controller an ID and instantiate that instead.
这篇关于使用导航控制器在 Storyboard 中呈现视图控制器 - Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!