Objective C:何时在 App Delegate 中使用方法以及何时在 View Controller 中使用方法
我对视图控制器和应用程序委托类中的以下方法有点困惑
应用程序委托中的方法:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
ViewController 中的方法:
- (void)viewDidLoad
在什么情况下我需要在应用程序委托或 ViewController 方法中添加代码?我相信为了切换视图,我们需要将其包含在应用程序委托方法中,是否有我们需要遵守的经验规则?
谢谢!
珍
I am a little confused on the following methods in both my View Controller and App delegate classes
Method in App delegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
Method in ViewController:
- (void)viewDidLoad
Under what situation do I need to add code in the app delegate or ViewController methods? I believe that for switching of views, we need to include it in the app delegate method, are there any rules of thumb that we need to abide by?
Thanks!
Zhen
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于在启动时调用,
application:didFinishLaunchingWithOptions:
通常包含初始化应用程序的逻辑(例如设置核心数据对象、注册推送通知等)。另一方面,viewDidLoad
的目的是在显示视图控制器之前初始化它。As it is called at launch,
application:didFinishLaunchingWithOptions:
typically contains logic to initialise your application (e.g. setting up core data objects, registering for push notifications, etc.). The purpose ofviewDidLoad
on the other hand is to initialise your view controller before it is shown.application:didFinishLaunchingWithOptions: 应用于应用程序启动时必须进行的设置,例如
viewDidLoad 应用于对于只需要为特定视图控制器完成的任何配置。在某些情况下,视图可能无法加载,因此在应用程序委托中进行该配置是没有意义的。
例如,
application:didFinishLaunchingWithOptions: should be used for setup that must occur when the application is launched, e.g.
viewDidLoad should be used for any configuration that only needs to be done for that specific view controller. In some cases the view may not get loaded, so there's no point doing that configuration in the app delegate.
e.g