简体   繁体   English

如何使用故事板中的视图以编程方式创建UIViewController

[英]How to Create a UIViewController programmatically with view from storyboard

I have a view controller designed in Main.storyboard named ProfileViewController also a corresponding class by that name. 我在Main.storyboard设计了Main.storyboard名为ProfileViewController的视图控制器,该名称也对应于该类。 I can instantiate the viewController like this, 我可以像这样实例化viewController,

UIStoryboard(name: "SignUp", bundle: nil).instantiateViewController(withIdentifier: "ProfileViewController") as? ProfileViewController

However what I'd like to do is instantiate the view controller like this 但是我想做的是像这样实例化视图控制器

let profileViewController = ProfileViewController(/*argument list*/)

Storyboard's view controllers MUST instantiate from storyboard . 情节提要的视图控制器必须storyboard提要实例化。 if you need a custom constructor function (like a initializer), you can define a simple one like this: 如果需要自定义构造函数(如初始化程序),则可以定义一个简单的函数,如下所示:

extension ProfileViewController {
    static func new(argument: any) -> ProfileViewController {
        let vc = UIStoryboard(name: "SignUp", bundle: nil).instantiateViewController(withIdentifier: "ProfileViewController") as! ProfileViewController
        vc.argument = argument
        return vc
    }
}

Note that if you want to use a real initializer, you can't use storyboard. 请注意,如果要使用真正的初始化程序,则不能使用情节提要。 You may use the view of the designed view controller and assign it to the view in initializer but you will loose other view controller settings. 您可以使用设计的视图控制器的视图,并将其分配给初始化程序中的视图,但是您将失去其他视图控制器设置。

extension ProfileViewController {
    convenience init(argument: Any) {
        self.init()
        self.view = UIStoryboard(name: "SignUp", bundle: nil).instantiateViewController(withIdentifier: "ProfileViewController").view
        self.argument = argument
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在故事板上创建一个Segue,从编程注册的UICollectionViewCell到新的UIViewController? - How to create a Segue on the Storyboard, from a programmatically registered UICollectionViewCell to a new UIViewController? 如何从故事板以编程方式加载 UIViewController? - How to load UIViewController programmatically from storyboard? 如何以编程方式从UINavigationController(storyboard)到UIViewController - How to segue from UINavigationController (storyboard) to UIViewController programmatically 在带有情节提要的项目中以编程方式创建UIViewController - Programmatically create a UIViewController in a project with a storyboard 从UITabbarItem和Storyboard创建一个UIViewController - Create a UIViewController from UITabbarItem and Storyboard 在Storyboard中以编程方式访问UIViewController - Accessing UIViewController programmatically in Storyboard 从情节提要UIViewController将视图加载到外部显示 - Load view from storyboard UIViewController to external display 如何在Storyboard中创建一个单独的视图以编程方式包含在UITableView中? - How to create a separate view in Storyboard to be included programmatically in UITableView? 以编程方式从情节提要中显示当前视图 - Present View From Storyboard Programmatically 如果不构建视图,是否无法以编程方式创建UIViewController? - Is it not possible to create a UIViewController programmatically without building the view?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM