iOS difference between AppDelegate and ViewController class [closed]

给你一囗甜甜゛ 提交于 2020-01-01 17:25:08

问题


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.



来源:https://stackoverflow.com/questions/18454297/ios-difference-between-appdelegate-and-viewcontroller-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!