且构网

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

使用导航控制器在 Storyboard 中呈现视图控制器 - Swift

更新时间:2023-02-12 12:30:51

你绝对是在正确的轨道上.不幸的是,当您通过情节提要 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.