iOS在AppDelegate和ViewController类之间的区别 [英] iOS difference between AppDelegate and ViewController class

查看:83
本文介绍了iOS在AppDelegate和ViewController类之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何知道AppDelegate类中包含什么代码,ViewController类中包含什么代码.这两个类在UIViewController中的viewDidLoadUIApplicationDelegate中的applicationDidFinishLaunching:都有相似的方法.我想applicationDidFinishLaunching:首先会被调用,这就是您创建视图的地方.但是有人可以解释一下每个类中的通用代码吗?

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?

推荐答案

简单来说,应用程序委托是特殊/不同,因为它可以获取诸如didFinishLaunchingWithOptions之类的应用程序方法,或applicationWillTerminate.您想将其用于整个应用程序设置或准备终止应用程序,即保存需要重用的对象,清除不必要的对象等,而不是将所有内容放置在此处而不是关联的视图控制器中.看来您混淆了viewDidLoaddidFinishLaunchingWithOptions.第一次加载视图控制器时,会调用viewDidLoad,整个应用程序加载完成后,会调用didFinishLaunchingWithOptions.

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.

这篇关于iOS在AppDelegate和ViewController类之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆