简体   繁体   English

如何在没有情节提要的情况下以编程方式实例化视图控制器

[英]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.

相关问题 实例化视图 controller 在 storyboard 从 storyboard 少视图 Z594C103F2C6E010C3D8AB059CF - Instantiate view controller created in storyboard from a storyboard less view controller 如何以编程方式访问视图控制器的故事板 ID - How can I access a view controller's storyboard id programatically iOS Storyboard:无法正确实例化视图控制器 - iOS Storyboard: Not able to instantiate view controller properly swift:如何设置类来查看没有故事板的控制器? - swift: How to set class to view controller without storyboard? Xcode 8:如何在不使用Segue或Storyboard的情况下将数据传递给视图控制器? - Xcode 8: How to pass data to a view controller without using a segue or storyboard? 从情节提要实例化视图控制器时出现XCTestCase错误 - XCTestCase error when instantiate view controller from storyboard 如何以编程方式快速创建 uicollectionview(没有故事板) - how to create uicollectionview with swift programatically (without storyboard) 如何将情节提要链接到视图控制器 - How to link storyboard to view controller 如何将数据从集合视图单元传递到新的视图控制器(以编程方式且没有情节提要)? - How can I passed data from collection view cell to new view controller (programmatically and without storyboard)? 如何在不使用情节提要或IB的情况下从嵌入式集合视图的单元导航到另一个视图控制器? - How to navigate from an embedded collection view’s cell to another view controller without using Storyboard or IB?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM