6

What is the difference between the two methods didFinishLaunchingWithOption and viewDidLoad?

The former is a method of AppDlegate.m and the latter is a method of ViewController.m, but both of them perform the same mission of loading the UIs onto the view.

David Nehme
  • 21,379
  • 8
  • 78
  • 117
JackieLam
  • 668
  • 1
  • 7
  • 20

1 Answers1

5

The application:didFinishLaunchingWithOptions: is a UIApplicationDelegate protocol method that gets called when iOS has finished setting up an area for your App to run and is the insertion point for you, the developer, to load a view controller, etc.

The viewDidLoad method on the other hand is a method of the UIViewController class that gets called when an instance of UIViewController gets its view loaded into memory. From Apple's documentation:

Called after the controller’s view is loaded into memory.

Discussion This method is called after the view controller has loaded its view hierarchy into memory. This method is called regardless of whether the view hierarchy was loaded from a nib file or created programmatically in the loadView method. You usually override this method to perform additional initialization on views that were loaded from nib files.

Beltalowda
  • 4,558
  • 2
  • 25
  • 33
  • but if I want to load a UIButton onto the view, I can put the code in both the viewDidLoad and the didFinishLaunchingWithOptions method. Is that right? – JackieLam Oct 24 '12 at 16:21
  • 9
    No, didFinishLaunchingWithOptions is for Application level setup. If you need to modify the subviews for a particular view controller then you need to do that in viewDidLoad. The didFinishLaunchingWithOptions method has a twenty (20) second limitation on its total execution time, after 20 seconds your App gets killed by iOS so it's a good idea to get in the habit of avoiding putting code in this method if it can go elsewhere. – Beltalowda Oct 24 '12 at 16:22
  • That's really useful and especially the 20 seconds stuff. Thx very much! – JackieLam Oct 24 '12 at 16:31