Reputation: 751
I think I might ask a fool question but it does confuse me for a while.
I am a beginner for iOS developing and the example I have seen online that people always write code inside the viewController's class.
However, according to my experience in C++, I think a class is just a template for reuse. You can only use it when it has been initialized. So the thing that performs the work is the instance.
My question would be when/who create the viewController instance in an app?
Upvotes: 1
Views: 51
Reputation: 138
I imagine you are referring to the template ViewController
, the one that always comes with a new single page application project, for instance.
It's being created "under the hood" once your app finishes launching, but what it is basically doing is the following:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController = ViewController();
return true
}
In fact, that's what you need to do if you want to delete your storyboard and work fully programmatically, in addition to clearing the Main Interface info in you project's general info as follows:
And if you want to show another custom ViewController
class, you can present it from another ViewController
as in
let secondViewController = MyCustomViewController()
// this line will place the MyCustomViewController instance on top of the current ViewController
present(secondViewController, animated: true, completion: nil)
I hope I could help!
Upvotes: 3