1

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?

5
  • 1
    Are you doing everything in code or do you use storyboards? Commented Jun 28, 2017 at 18:40
  • @rmaddy Probably storyboards. If OP was doing everything in code, then they would see how VCs are instantiated because they would be doing it themselves Commented Jun 28, 2017 at 18:46
  • This is a very good question. I had confusion this myself, when I first started out Commented Jun 28, 2017 at 18:46
  • @Alexander Yeah, that occurred to me seconds after posting my comment. Commented Jun 28, 2017 at 18:46
  • The View Controller Programming Guide for iOS will probably help. Commented Jun 28, 2017 at 18:47

1 Answer 1

3

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:

Leave the Main Interface info blank

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!

Sign up to request clarification or add additional context in comments.

2 Comments

Doesn't the Main Interface only specify a xib/nib/storyboard? The instantiation of the VC is then done from there. I can't check myself right now
Yeah, that's exactly it. I pointed it out just in case he wants to try running a ViewController without storyboard. If it's not blank, it won't work (at least until Xcode 8.3)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.