Load UIViewController from the separate nib file in swift? Load UIViewController from the separate nib file in swift? ios ios

Load UIViewController from the separate nib file in swift?


var viewController = OfferDetailViewController(nibName: "OfferDetailViewController", bundle: nil)


Here is a nice generic approach...

extension UIViewController {    class func loadFromNib<T: UIViewController>() -> T {         return T(nibName: String(describing: self), bundle: nil)    }}let vc : OfferDetailViewController = OfferDetailViewController.loadFromNib()


solution with type casting:

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

enjoyment:

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


matomo