xcode AppDelegate.m 和 View Controller.m 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6062569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between AppDelegate.m and View Controller.m
提问by Legolas
Could anyone tell me when we use the AppDelegate.m and AppDelegate.h during iPhone programming? I have used only the ViewController.m and ViewController.h for basic learning. I just want to know when and why AppDelegate is used.
谁能告诉我在 iPhone 编程期间我们何时使用 AppDelegate.m 和 AppDelegate.h?我只使用了 ViewController.m 和 ViewController.h 进行基本学习。我只想知道何时以及为何使用 AppDelegate。
回答by Caleb
Both define classes, but the classes are used for different things. ViewController.h/m define a view controller class that manages a hierarchy of views -- basically, one screen of an application. You might have multiple screens that each have their own view controller.
两者都定义了类,但是这些类用于不同的事情。ViewController.h/m 定义了一个视图控制器类,用于管理视图层次结构——基本上是应用程序的一个屏幕。您可能有多个屏幕,每个屏幕都有自己的视图控制器。
AppDelegate.h/m define a class that manages the application overall. The app will create one instance of that class and send that object messages that let the delegate influence the app's behavior at well-defined times. For example, -application:didFinishLaunchingWithOptions: is sent when the app has finished launching and is ready to do something interesting. Take a look at the UIApplicationDelegate reference pagefor a list of messages that the app delegate can implement to modify the behavior of the application.
AppDelegate.h/m 定义了一个整体管理应用程序的类。应用程序将创建该类的一个实例并发送该对象消息,让委托在明确定义的时间影响应用程序的行为。例如,-application:didFinishLaunchingWithOptions: 当应用程序完成启动并准备做一些有趣的事情时发送。查看UIApplicationDelegate 参考页面,了解应用程序委托可以实现以修改应用程序行为的消息列表。
回答by octy
I would like to add the following to @Caleb's answer.
我想在@Caleb 的回答中添加以下内容。
If care is not taken, the AppDelegate could easily become one of the most accessed objects in the application. I usually refrain from calling methods in the AppDelegate from any of my ViewControllers. Unless, something needs to be reported to the AppDelegate that would influence the behaviour of the whole application.
如果不小心,AppDelegate 很容易成为应用程序中访问最多的对象之一。我通常避免从我的任何 ViewController 调用 AppDelegate 中的方法。除非,需要向 AppDelegate 报告会影响整个应用程序行为的某些内容。
I keep my AppDelegate for the following:
我保留我的 AppDelegate 如下:
- initialization: whatever needs to be done on the very first launch (after an install or an update)
- data migration from version to version (e.g. if you use CoreData and migrations)
- configuration of objects linked via IBOutlets from MainWindow.xib
- determining the initial orientation to launch in
- saving uncommitted data / state prior to the application being terminated or entering background mode
- registering for the Apple Push Notification Service and sending the device token to our server
- opening one of the supported application URLs (e.g. maps://)
- 初始化:在第一次启动时(安装或更新后)需要做的任何事情
- 从版本到版本的数据迁移(例如,如果您使用 CoreData 和迁移)
- 通过来自 MainWindow.xib 的 IBOutlets 链接的对象的配置
- 确定发射的初始方向
- 在应用程序终止或进入后台模式之前保存未提交的数据/状态
- 注册 Apple 推送通知服务并将设备令牌发送到我们的服务器
- 打开支持的应用程序 URL 之一(例如 maps://)
For other use case scenarios and a more thourough description of the AppDelegate, see the iOS Application Programming Guide.
有关其他用例场景和 AppDelegate 的更详尽描述,请参阅iOS 应用程序编程指南。
回答by Francesco
The view-controller. h/m is responsible of controlling the connection between your model and your view (more on MVChere).
的视图-控制器。h/m 负责控制模型和视图之间的连接(更多关于MVC在这里)。
AppDelegate. h/m is responsible for the life-cycle of your application. What to do when the user press the home button and exit your app, what to do when the app enter background. Things like this.
应用程序委托。h/m 负责应用程序的生命周期。当用户按下主页按钮并退出您的应用程序时该怎么办,当应用程序进入后台时该怎么办。这样的事情。