I have the following class
CalendarViewController.swift
import UIKit
class CalendarViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
and I have CalendarViewController.xib
with file owner custom class is CalendarViewController
.
Based on https://stackoverflow.com/a/55801259/72437
I thought if I want to create view controller from XIB, I need to perform
extension UIViewController {
static func instanceFromNib() -> Self {
func instantiateFromNib<T: UIViewController>() -> T {
return T.init(nibName: String(describing: T.self), bundle: nil)
}
return instantiateFromNib()
}
}
let calendarViewController = CalendarViewController.instanceFromNib()
However, if I try the following code
let calendarViewController = CalendarViewController()
I notice that calendarViewController
is created from XIB! (Because it contains all the UI components from XIB)
I also notice that, if I remove CalendarViewController.xib
from the project, and run same code
let calendarViewController = CalendarViewController()
again, then the view controller will not created from XIB.
May I know, why UIViewController
is created from same name XIB even though empty constructor is used?