Custom init for UIViewController in Swift with interface setup in storyboard

后端 未结 12 2108
长情又很酷
长情又很酷 2020-11-27 05:08

I\'m having issue for writing custom init for subclass of UIViewController, basically I want to pass the dependency through the init method for viewController rather than se

12条回答
  •  感情败类
    2020-11-27 05:36

    Swift 5

    You can write custom initializer like this ->

    class MyFooClass: UIViewController {
    
        var foo: Foo?
    
        init(with foo: Foo) {
            self.foo = foo
            super.init(nibName: nil, bundle: nil)
        }
    
        public required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
            self.foo = nil
        }
    }
    

提交回复
热议问题