Loading ViewController from xib file

前端 未结 10 2392
我在风中等你
我在风中等你 2020-12-08 02:33

I had a MyViewController.swift and a MyViewController.xib presenting the layout of MyViewController.

I tried different methods to load this

10条回答
  •  南方客
    南方客 (楼主)
    2020-12-08 02:58

    extension UIViewController {
        static func loadFromNib() -> Self {
            func instantiateFromNib() -> T {
                return T.init(nibName: String(describing: T.self), bundle: nil)
            }
    
            return instantiateFromNib()
        }
    }
    

    Use it as the following:-

    let testVC = TestVC.loadFromNib()
    

提交回复
热议问题