简体   繁体   English

我如何保证在didFinishLaunchingWithOptions之前在视图控制器中注册通知?

[英]How can I guarantee registering for a notification happens in a view controller before didFinishLaunchingWithOptions?

Specifically, I want to register for a notification -before posted by the didFinishLaunchingWithOptions - in my main view controller so I can perform a task from the 'splash' (forgive me Ob-C nerds, forgot what its really called) screen without doing the ominous 具体来说,我想在主视图控制器中注册一个通知(在didFinishLaunchingWithOptions发布之前),这样我就可以从“启动”屏幕执行任务(原谅Ob-C书呆子,忘记了它的真正名称),而无需执行不祥的

[[[[[[UIApplication sharedApplication] delegate] window] rootViewController] topViewController] functionIwantToCallOnceAtTheStart];

Side note: I love the idea of having a 'global start whistle,' why is this not a default? 旁注:我喜欢“全球启动哨”的想法,为什么这不是默认设置?

Logging didFinishLaunchingWithOptions and the root view controller's delegate methods results in 记录didFinishLaunchingWithOptions和根视图控制器的委托方法会导致

2013-07-25 02:02:46.532 Tests[918:c07] awakeFromNib
2013-07-25 02:02:46.535 Tests[918:c07] didFinishLaunchingWithOptions
2013-07-25 02:02:46.538 Tests[918:c07] viewDidLoad
2013-07-25 02:02:46.539 Tests[918:c07] viewWillAppear
2013-07-25 02:02:46.540 Tests[918:c07] viewWillLayoutSubviews
2013-07-25 02:02:46.543 Tests[918:c07] viewDidLayoutSubviews
2013-07-25 02:02:46.544 Tests[918:c07] viewWillLayoutSubviews
2013-07-25 02:02:46.545 Tests[918:c07] viewDidLayoutSubviews
2013-07-25 02:02:46.549 Tests[918:c07] viewDidAppear

but is it guaranteed that awakeFromNib will always come before didFinishLaunchingWithOptions, and on all iOS devices/versions? 但是是否可以保证awakeFromNib始终在didFinishLaunchingWithOptions之前以及在所有iOS设备/版本上出现?

This question has a helpful answer but my project is using storyboards, and I am unsure of the implications of setting the rootViewController manually with a storyboard. 这个问题有一个有用的答案,但是我的项目正在使用情节提要,并且我不确定用情节提要手动设置rootViewController的含义。

Edit : 编辑

I originally asked this question because I wanted to have the launch image animate away. 我最初问这个问题是因为我想使启动图像动起来。 I realize there are better methods to do this, but I still want to use the "Application Started" notification to launch a network request to populate my main view controller (a UITableViewController) as soon as the app is started. 我意识到有更好的方法可以执行此操作,但是我仍然想使用“ Application Started”通知来启动网络请求,以便在应用程序启动后立即填充我的主视图控制器(UITableViewController)。

awakeFromNib is guaranteed to be called on the initial view controller of your storyboard before applicationDidFinishLaunching is called. 确保在调用applicationDidFinishLaunching之前在情节awakeFromNib的初始视图控制器上调用awakeFromNib Loading from the nib is part of the launch. 从笔尖加载是发射的一部分。

However, its not really clear from the question what you're trying to do. 但是,从这个问题中并不清楚您要做什么。 While the launch image is displayed, your view controller isn't really there, so what are you asking it to do? 显示启动图像时,您的视图控制器实际上并不在那里,那么您要它做什么呢? None of the UI objects will be present until it has loaded, so whatever task you're trying to do may not come under the responsibility of a view controller. 在加载之前,不会显示任何UI对象,因此,您要执行的任何任务都可能不会由视图控制器负责。

If you really really want to run some code, you can open up the main class. 如果您真的想运行一些代码,则可以打开主类。 It's in your project under supporting files. 它在您的项目中的支持​​文件下。

Also, this guide explains quite a bit on the appication startup sequence: 此外,本指南还对应用启动顺序进行了很多解释:

http://oleb.net/blog/2012/02/app-launch-sequence-ios-revisited/ http://oleb.net/blog/2012/02/app-launch-sequence-ios-revisited/

The point of the launch image is to make your app appear to launch faster than it really does by putting a static image on screen as soon as possible. 启动图像的目的是通过尽快在屏幕上放置静态图像,使您的应用程序启动看起来比实际启动更快。 As long as your launch image looks exactly like whatever view your initial view controller shows, nobody will know the difference. 只要您的启动图像看起来与初始视图控制器显示的视图完全一样,就不会有人知道其中的区别。 If you want that initial image to animate away somehow, no problem: 如果您希望该初始图像以某种方式产生动画效果,那么没问题:

  • Create an initial view controller that displays your image and does a custom transition to your next view controller. 创建一个显示图像的初始视图控制器,并自定义过渡到下一个视图控制器。

  • If the image you're displaying covers the screen exactly, you can also use that image as the launch image. 如果您要显示的图像正好覆盖了屏幕,您也可以将该图像用作启动图像。 Otherwise, run the app and do a screen capture when the first view controller's view appears; 否则,请运行该应用程序并在第一个视图控制器的视图出现时进行屏幕截图; make that screen shot your launch image. 使该屏幕截图成为您的启动图像。

  • Put some code in your -application:didFinishLaunchingWithOptions: method to start the transition from the initial view controller to the next one. 将一些代码放在-application:didFinishLaunchingWithOptions:方法中,以开始从初始视图控制器到下一个视图控制器的转换。

That should do it. 那应该做。 Remember that the launch image is a sort of implementation detail -- the user isn't supposed to be aware of it as a separate thing, so it's natural to do what you're trying to do as a view controller transition instead of fooling around with the launch image itself. 请记住,启动映像是一种实现细节,用户不应将其视为独立的东西,因此很自然地执行视图控制器转换时要执行的操作,而不是无所事事与启动图像本身。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 application:didFinishLaunchingWithOptions:在创建目标控制器之前触发通知 - application:didFinishLaunchingWithOptions: firing notification before destination controller is created 如何召回didFinishLaunchingWithOptions内部的对象? - How can I recall a object inside didFinishLaunchingWithOptions? 在将图像加载到视图控制器之前,如何裁剪图像 - How can I crop an image before loading it to the view controller 在AppDelegate中的didFinishLaunchingWithOptions之前初始化控制器负载 - is initialize controller load before the didFinishLaunchingWithOptions in AppDelegate 从didFinishLaunchingWithOptions中的其他视图控制器中调用方法 - Calling a method from a different view controller in didFinishLaunchingWithOptions 如何在顶视图控制器中直接处理iOS推送通知? - How can I handle iOS push notification directly in my top view controller? 在继续之前,我如何保证 Swift 闭包(引用 Firebase)完全执行? - How can I guarantee that a Swift closure (referencing Firebase) fully executes before I move on? 在Storyboard应用程序上,如何在应用程序期间显示来自AppDelegate的视图控制器:didFinishLaunchingWithOptions :? - On a Storyboard application, how to present a view controller from the AppDelegate during the application:didFinishLaunchingWithOptions:? 如何从本地通知中打开特定的视图控制器? - How do I open a specific view controller from local notification? iOS 开发:如何在将视图控制器推送到导航堆栈之前预加载它? - iOS Development: How can I preload a view controller before pushing it onto the navigation stack?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM