简体   繁体   English

不使用故事板时使用 Swift 初始化 UIViewController 的正确方法是什么?

[英]What is the proper way to initialize a UIViewController using Swift when not using Storyboards?

I want to get rid of the Storyboards in my project.我想摆脱项目中的故事板。 Everything is written in code but I still have UIViewController(s) as reference.一切都是用代码编写的,但我仍然有 UIViewController(s) 作为参考。

When trying to present a UIViewController I normally use the method shown below and I'm able to access variables for that particular UIViewController.当尝试显示 UIViewController 时,我通常使用下面显示的方法,并且我能够访问该特定 UIViewController 的变量。

func presentAttributeOptions(_ attribute: Attribute, controller: ProductViewController) {

    let storyboard = UIStoryboard(name: Storyboards.Product, bundle: nil)
    let destination = storyboard.instantiateViewController(withIdentifier: Controllers.Attributes) as! AttributeOptionsViewController

    destination.providesPresentationContextTransitionStyle = true
    destination.definesPresentationContext = true
    destination.modalPresentationStyle = .overFullScreen
    destination.attribute = attribute
    destination.selectOptionDelegate = controller
    present(destination, animated: true, completion: nil)
}

However, removing the reference to the Storyboard and using the method shown below, I get errors such as for this example "Value of type 'ProductViewController' has no member 'attribute'" and "Value of type 'ProductViewController' has no member 'selectOptionDelegate'"但是,删除对 Storyboard 的引用并使用下面显示的方法,我收到错误,例如“'ProductViewController' 类型的值没有成员'属性'”和“'ProductViewController' 类型的值没有成员'selectOptionDelegate” '"

func presentAttributeOptions(_ attribute: Attribute, controller: ProductViewController) {

    let destination = ProductViewController()

    destination.providesPresentationContextTransitionStyle = true
    destination.definesPresentationContext = true
    destination.modalPresentationStyle = .overFullScreen
    destination.attribute = attribute
    destination.selectOptionDelegate = controller
    present(destination, animated: true, completion: nil)
}

I found several answers but none seemed to work like for example adding the following init() methods shown below to the UIViewController. What am I missing?我找到了几个答案,但似乎没有一个有效,例如将下面显示的以下 init() 方法添加到 UIViewController。我错过了什么?

// This allows you to initialize your custom UIViewController without a nib or bundle.
convenience init() {
    self.init(nibName: nil, bundle: nil)
}

// This extends the superclass.
override init(nibName: String?, bundle: Bundle?) {
    super.init(nibName: nil, bundle: nil)
}

// This is also necessary when extending the superclass.
required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented") // or see Roman Sausarnes's answer
}

I just see this:我只看到这个:

  • with storyboard, destination is of type AttributeOptionsViewController storyboard,目标是 AttributeOptionsViewController 类型
  • without, destination is of type ProductionViewController没有,目标是 ProductionViewController 类型

That may be a cause这可能是一个原因

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM