Let's say I have custom view controllers which have their own .xib file setted properly.
I also have this extension
class func fromNib<T: UIViewController>() -> T { return T(nibName: String(describing: T.self), bundle: nil) } Let's call our custom UIViewController VC.
let customVC = VC() let customVC:VC = .fromNib() What is the main difference between the top 2 approaches ? The funny thing is I used both of them, and both work, but not always and not for all of them. Using the first approach I managed to find that whenever you add the controller as a child, it's loadView and lifecycle starts. Using the second approach you have to implement the following 2 things:
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) required init?(coder aDecoder: NSCoder) I tried replacing everything that used to have .fromNib with the first approach, in order to skip implementing those methods above, and just doing everything inside viewDidLoad.
Why does the first approach work flawlessly for some controllers, yet for others the outlets are not being connected ?