【问题标题】:How to instantiate a view controller programatically, without storyboard如何在没有情节提要的情况下以编程方式实例化视图控制器
【发布时间】:2017-03-29 05:22:38
【问题描述】:

有没有办法在不使用情节提要标识符的情况下以编程方式实例化视图控制器。我想将 CNContactPickerController 推送到我的根视图控制器中。

let controller = CNContactPickerViewController()
controller.delegate = self
navigationController?.present(controller, animated: true, completion: nil)

我这样做了,但是在contactPicker(代表)中,我从情节提要中推送了一个VC,我在其中保存了我想要从该特定联系人那里获得的信息。 问题:当我弹出最后一个视图控制器时,我想继续使用 CNConctactPickerControllerView 但我继续使用第一个视图控制器 我尝试了解雇,但没有任何反应..

【问题讨论】:

    标签: swift contactpicker


    【解决方案1】:

    这样做的问题是为您的视图控制器创建了 NONE 的 UI。您所做的只是实例化该类的一个实例。没有创建相应的 UI,您的 IBOutlets 也将为零。

    您有 2 个选项,您可以使用界面构建器并从 Nib 或 Storyboard 实例化,或者您在代码中手动创建 ALL 您的 UI。

    为了可重复使用,您可以在CNContactPickerViewController 上创建一个静态方法来为您处理情节提要实例化。请参阅以下内容:

    class CBContactPickerViewController: UIViewController {
    
        static func fromStoryboard() -> CNContactPickerViewController {
            return UIStoryboard(name: "foobar", bundle: nil).instantiateViewController(identifier: "CNContactPickerViewController") as! CNContactPickerViewController
        }
    }
    

    然后您可以按如下方式使用它:

    self.present(viewController: CNContactPickerViewController.fromStoryboard(), animated: true, completion: nil)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-19
      • 2016-09-13
      • 2016-07-21
      • 1970-01-01
      • 1970-01-01
      • 2018-04-10
      相关资源
      最近更新 更多