i wonder how storyboards and class are connected. I deleted all "main" mentions in target/project and info.plist. and set rootViewController in SceneDelegate:
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
window?.rootViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Dad")
window?.makeKeyAndVisible()
my storyBoard:
ViewController:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .brown
}
}
what I get:
Question is: If I initializing Root ViewController from Storyboard in SceneDelegate. How it knows that background should be brown? I know that I picked ViewController as a Custom Class in Identity Inspector, but How it works under the hood? like when wrote some code in
class ViewController
it goes into nib file too? How this interaction between storyboard and class works?
p.s: sorry for my english
CodePudding user response:
it goes into nib file too?
No. The nib file only has what you put in the storyboard.
I know that I picked
ViewController
as a Custom Class in Identity Inspector, but How it works under the hood?
When you did this in your code:
UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Dad")
the nib is read, and iOS now sees that the VC with the identifier "Dad" has the class ViewController
, so it makes an instance of that class by calling the init(coder:)
initialiser, so that the things stored in the nib can be decoded, followed by a bunch of other lifecycle methods including but not limited to awakeFromNib
, viewDidLoad
, viewWillAppear
, viewDidAppear
, etc.
Since viewDidLoad
of your class is called at some point, the background is set to brown.