I have a view controller designed in Main.storyboard
named ProfileViewController
also a corresponding class by that name. I can instantiate the viewController like this,
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
. 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
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.