Load UIViewController from the separate nib file in swift?

后端 未结 3 794
悲哀的现实
悲哀的现实 2021-02-02 10:39

I had take one ViewController with separate nib file. and my initial root viewcontroller is set in the storyBoard. Now the problem is that when I push to this controller the Vi

3条回答
  •  春和景丽
    2021-02-02 11:04

    solution with type casting:

    extension UIViewController {
        static func initFromNib() -> Self {
            func instanceFromNib() -> T {
                return T(nibName: String(describing: self), bundle: nil)
            }
            return instanceFromNib()
        }
    }
    

    enjoyment:

    let testVC = TestVC.initFromNib()
    testVC.someCustomParam = "someValue"
    

提交回复
热议问题