且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用导航控制器在故事板中呈现视图控制器 - Swift

更新时间:2022-12-30 11:29:16

你肯定是在正确的轨道上。不幸的是,当您通过其故事板ID引用视图控制器时,它将忽略它嵌入任何内容的事实。对于segue,当你转向嵌入的东西时,目标视图控制器将是嵌入控制器,而不是你通常感兴趣的控制器。无论如何,你应该能够以类似的方式修复问题。 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.