-->

iOS difference between AppDelegate and ViewControl

2019-04-13 01:29发布

问题:

How do I know what code goes in the AppDelegate class and what code goes in the ViewController class. Both classes have a similar method of viewDidLoad in UIViewController and applicationDidFinishLaunching: in UIApplicationDelegate. I guess applicationDidFinishLaunching: would be called first and that's where you'd create your view. But could someone please explain what general code goes in each of those classes?

回答1:

To put it simply, the App Delegate is special/different in the sense that it gets application methods such as didFinishLaunchingWithOptions, or applicationWillTerminate. You want to use it for entire app setup or for preparation to terminate your app i.e. saving objects that need to be reused, clearing unnecessary objects, etc, vs just putting everything in there instead of the correlating view controller. It appears you confused viewDidLoad and didFinishLaunchingWithOptions. viewDidLoad gets called when the view controller loads for the first time, didFinishLaunchingWithOptions gets called when the whole application is done loading.



回答2:

AppDelegate is used for the whole app, you can use it to manage the app life cycle, on the other hand, ViewController is used for a single view. you can use it to manage life cycle of a view. One app can have multiple views. but only one AppDelegate.