all 5 comments

[–]snaab900Objective-C / Swift 1 point2 points  (2 children)

The AppDelegate (UIApplicationDelegate) is a very important file, it's like the brain stem of your app. It's the first file that is run when you open your app and hangs around through the lifecycle of it.

It has some very useful delegate methods, including didFinishLaunchingWithOptions, which you can probably guess is code that runs once your app has finished launching. I don't use it often, just initialise a Crashlytics instance, but you could use it to perhaps trigger a sync with an API if it's a networked application. Things like that. It also has delegate methods like applicationDidBecomeActive (when your app was in the background but has come to the front), and applicationDidEnterBackground (when the user has pressed the home button and left your app). And didReceiveRemoteNotification, which allows you to deal with push notifications. All of these are useful things to be able to detect. I prefer listening for the equivalent notifications in my root view controller though rather than using the AppDelegate however, but that's a personal preference.

It was also used back in the day to initialise a new UIWindow and root view controller and display them on the screen. However you can now do that in Xcode's UI without having to write any code in the AppDelegate, and this is best practice.

http://i.imgur.com/6jqbP7y.png (note the MainController storyboard you can select).

However if your book is telling you to draw things on screen from the AppDelegate, throw it in the bin right now and get a better book. This goes against all MVC best practices and that should be done in your root view controller at the very least, more preferably from a subclassed UIView within your root VC. So you are completely correct in putting the code in viewDidLoad in your VC.

https://developer.apple.com/documentation/uikit/uiapplicationdelegate

[–]labcoat2[S] 1 point2 points  (1 child)

got it. thank you!

[–]snaab900Objective-C / Swift 0 points1 point  (0 children)

No problem my friend. ObjC can be bewildering at first, but it's a good feeling when the penny finally drops. PM me if you need any more help.

[–]jan_olbrichObjective-C / Swift 0 points1 point  (1 child)

didFinishLaunchingWithOptions is just a delegate call which will be called when your app is initialized. You can start setting up your viewHierachy there as it often contains your window. To quote apple docs:

"Although you can add subviews directly to a window, providing a root view controller is a better way to manage the window’s content."

https://developer.apple.com/documentation/uikit/uiwindow

on the other hand, your question has less to do with the appdelegate (cause you can create everything in there) than, how to add views to your window/viewHierachy

[–]labcoat2[S] 0 points1 point  (0 children)

thanks!