[英]How to create a Segue on the Storyboard, from a programmatically registered UICollectionViewCell to a new UIViewController?
[英]How to Create a UIViewController programmatically with view from storyboard
我在Main.storyboard
設計了Main.storyboard
名為ProfileViewController
的視圖控制器,該名稱也對應於該類。 我可以像這樣實例化viewController,
UIStoryboard(name: "SignUp", bundle: nil).instantiateViewController(withIdentifier: "ProfileViewController") as? ProfileViewController
但是我想做的是像這樣實例化視圖控制器
let profileViewController = ProfileViewController(/*argument list*/)
情節提要的視圖控制器必須從storyboard
提要實例化。 如果需要自定義構造函數(如初始化程序),則可以定義一個簡單的函數,如下所示:
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
}
}
請注意,如果要使用真正的初始化程序,則不能使用情節提要。 您可以使用設計的視圖控制器的視圖,並將其分配給初始化程序中的視圖,但是您將失去其他視圖控制器設置。
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.