[英]How to instantiate a view controller programatically, without storyboard
Is there a way to instantiate a view controller programatically without using storyboard identifier. 有没有一种方法可以在不使用情节提要标识符的情况下以编程方式实例化视图控制器。 I want to push CNContactPickerController into my root view controller . 我想将CNContactPickerController推送到我的根视图控制器中。
let controller = CNContactPickerViewController()
controller.delegate = self
navigationController?.present(controller, animated: true, completion: nil)
i did this but in contactPicker (delegate) i push a VC from storyboard where i save what info i want from that specific contact. 我做到了,但是在contactPicker(委托)中,我从情节提要中推送了VC,在其中保存了我想要从该特定联系人获得的信息。 The problem: when i pop the last view controller i want to go on the CNConctactPickerControllerView but i go on the first view controller i tried with dismiss but nothing happens.. 问题:当我弹出最后一个视图控制器时,我想继续使用CNConctactPickerControllerView,但是我继续使用第一个视图控制器,尝试关闭,但没有任何反应。
The problem with doing this is that NONE of the UI for your view controller is created. 有这样做的问题在于,对于您的视图控制器的UI 没有被创建。 What you are doing is simply instantiating an instance of the class. 您正在做的只是简单地实例化该类的实例。 None of the corresponding UI is created, also your IBOutlet
s will be nil. 没有创建相应的UI,您的IBOutlet
也将为nil。
You have 2 options, either you use Interface builder and instantiate from the Nib or Storyboard, or you create ALL your UI manually in code. 您有2个选项,或者使用“界面”构建器并从Nib或Storyboard实例化,或者在代码中手动创建ALL UI。
For the sake of resuability you could create a static method on CNContactPickerViewController
to handle the storyboard instantiation for you. 为了可恢复性,您可以在CNContactPickerViewController
上创建一个静态方法来为您处理情节CNContactPickerViewController
实例化。 See the following: 请参阅以下内容:
class CBContactPickerViewController: UIViewController {
static func fromStoryboard() -> CNContactPickerViewController {
return UIStoryboard(name: "foobar", bundle: nil).instantiateViewController(identifier: "CNContactPickerViewController") as! CNContactPickerViewController
}
}
You can then utilise this as follows: 然后,您可以如下利用它:
self.present(viewController: CNContactPickerViewController.fromStoryboard(), animated: true, completion: nil)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.