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

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

问题描述

我如何知道 AppDelegate 类中有哪些代码以及 ViewController 类中有哪些代码.这两个类在 UIViewControllerapplicationDidFinishLaunching: 中有一个类似的方法 viewDidLoadUIApplicationDelegate.我猜 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?

推荐答案

简单地说,App Delegate 是特殊/不同的,因为它获得了诸如 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.

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

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