草庐IT

objective-c - 'applicationDidBecomeActive' 之后调用了什么方法?

coder 2024-01-10 原文

这是我的第一个问题,因为我在开发我的第一个 iOS 应用程序时遇到了问题。它是数以千计的手电筒应用程序之一,但我正在尝试为其添加尽可能多的功能。其中之一是在应用程序进入后台或终止时保存应用程序的状态。转到前台(iOS 4 或更高版本)或重新启动后,我从文件加载设置并重新应用它们。显然,其中一项设置是 AVCaptureDevice.torchMode。但是,我遇到了这个问题。我在 applicationDidBecomeActive 方法中重新应用这些设置。这一切似乎都有效,但是当我快速点击主页按钮然后重新启动应用程序时,该应用程序将执行以下操作(我延迟了 applicationDidBecomeActive 方法来观察它):

1.显示黑屏(加载中)
2。执行 applicationDidBecomeActive 并打开 LED(我在这里设置延迟)
3.显示我当前的 UIViewController 并同时关闭 LED

只有在应用程序发送到那里后立即从后台调用应用程序后才会发生。我知道这不是现实的用例场景,但我喜欢认为错误经常“堆积”并且由于这种(可能)糟糕的设计,我将来可能会遇到其他问题。我绝对确定这不是我关闭 LED 的代码,因为每当我的代码修改 AVCaptureDevice.torchMode 属性时,我都会使用 NSLog。所以,准确地说,我的问题是:
applicationDidBecomeActive 之后调用什么方法(可能与 UIViewController 相关)可以关闭手电筒?是否有任何可能的解决方案或解决方法?

最佳答案

根据iOS App Programming guide :

Returning to the foreground is your app’s chance to restart the tasks that it stopped when it moved to the background. The steps that occur when moving to the foreground are shown in Figure 3-6. The applicationWillEnterForeground: method should undo anything that was done in your applicationDidEnterBackground: method, and the applicationDidBecomeActive: method should continue to perform the same activation tasks that it would at launch time.

您是否尝试过在 applicationDidBecomeActive: 方法而不是 applicationWillEnterForeground: 中重新应用您的设置?

另一件需要考虑的事情是使用通知:

在 AppDelegate 的 applicationDidBecomeActive:applicationDidBecomeActive: 方法中,您可以告诉您的应用委托(delegate)向您的 Controller 发送通知:

- (void)applicationDidBecomeActive:(UIApplication *)application {
    /*
     Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
     */

    // Dispatch notification to controllers
    [[NSNotificationCenter defaultCenter] postNotificationName: @"didBecomeActive" 
                                                        object: nil 
                                                      userInfo: nil];
}

一旦有了这个, View Controller 就可以像这样注册这些通知(例如,在它们的 init 方法中):

[[NSNotificationCenter defaultCenter] addObserver: self 
                                                 selector: @selector(loadSettings) 
                                                     name: @"didBecomeActive" 
                                                   object: nil];

这样,您的 Controller 就会知道该应用程序刚刚激活并可以执行您想要的任何方法。

在此示例中,您告诉 View Controller 在收到 didBecomeActive 通知(由应用委托(delegate)发布)时执行 loadSettings 方法。

关于objective-c - 'applicationDidBecomeActive' 之后调用了什么方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8387048/

有关objective-c - 'applicationDidBecomeActive' 之后调用了什么方法?的更多相关文章

  1. ruby - 如何使用 Nokogiri 的 xpath 和 at_xpath 方法 - 2

    我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div

  2. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  3. ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法? - 2

    类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc

  4. ruby - Facter::Util::Uptime:Module 的未定义方法 get_uptime (NoMethodError) - 2

    我正在尝试设置一个puppet节点,但ruby​​gems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由ruby​​gems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby

  5. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  6. ruby-on-rails - Rails - 子类化模型的设计模式是什么? - 2

    我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co

  7. ruby-on-rails - 'compass watch' 是如何工作的/它是如何与 rails 一起使用的 - 2

    我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t

  8. Ruby 方法() 方法 - 2

    我想了解Ruby方法methods()是如何工作的。我尝试使用“ruby方法”在Google上搜索,但这不是我需要的。我也看过ruby​​-doc.org,但我没有找到这种方法。你能详细解释一下它是如何工作的或者给我一个链接吗?更新我用methods()方法做了实验,得到了这样的结果:'labrat'代码classFirstdeffirst_instance_mymethodenddefself.first_class_mymethodendendclassSecond使用类#returnsavailablemethodslistforclassandancestorsputsSeco

  9. ruby - 什么是填充的 Base64 编码字符串以及如何在 ruby​​ 中生成它们? - 2

    我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%

  10. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

随机推荐