菜鸟教程小白 发表于 2022-12-11 17:02:36

ios - 使用 NSTimer 调用函数


                                            <p><p>如下所示,函数 loadView 3 应该在用户点击按钮后运行 300 秒(5 分钟)。但是当我构建和运行时,它没有。我也做了一些实验,我把定时器改成了 5 秒,它奏效了。那么在应用程序从 iOS 系统中挂起后,NSTimer 就不再运行了吗?那么问题出在哪里,我该如何解决呢?</p>

<pre><code>@IBAction func buttonTapped(sender: AnyObject) {
    NSTimer.scheduledTimerWithTimeInterval(300, target: self, selector: #selector(ViewController.loadView3), userInfo: nil, repeats: false)   
    createLocalNotification()
}

func createLocalNotification() {
    let localnotification = UILocalNotification()
    localnotification.fireDate = NSDate(timeIntervalSinceNow: 300)
    localnotification.applicationIconBadgeNumber = 1
    localnotification.soundName = UILocalNotificationDefaultSoundName
    localnotification.alertBody = &#34;Hello!&#34;
    UIApplication.sharedApplication().scheduleLocalNotification(localnotification)
}

func loadView3() {
    label.text = &#34;e89saa&#34;
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>你可以试试这样的。方法是,如果定时器工作以相同的逻辑进行,否则(可能应用程序被杀死或进入后台),在方法 viewWillAppear 中显示 Controller 之前保存firedDate并更新UI。</p>

<pre><code>@IBAction func buttonTapped(sender: AnyObject) {
    NSTimer.scheduledTimerWithTimeInterval(300, target: self, selector: #selector(ViewController.loadView3), userInfo: nil, repeats: false)   
    createLocalNotification()
}

func createLocalNotification() {
    let localnotification = UILocalNotification()
    localnotification.fireDate = NSDate(timeIntervalSinceNow: 300)
    localnotification.applicationIconBadgeNumber = 1
    localnotification.soundName = UILocalNotificationDefaultSoundName
    localnotification.alertBody = &#34;Hello!&#34;
    UIApplication.sharedApplication().scheduleLocalNotification(localnotification)

    // save in UserDefaults fireDate
    let defaults = NSUserDefaults.standardUserDefaults()
    defaults.setObject(localnotification.fireDate, forKey: &#34;firedDate&#34;)
    defaults.synchronize()
}

func loadView3() {
    // reset in UserDefaults fireDate
    let defaults = NSUserDefaults.standardUserDefaults()
    defaults.removeObjectForKey(&#34;firedDate&#34;)
    defaults.synchronize()

    label.text = &#34;e89saa&#34;
}

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated) // No need for semicolon

    // retrieve fireDate from UserDefaults
    let defaults = NSUserDefaults.standardUserDefaults()
    let fireDate = defaults.objectForKey(&#34;firedData&#34;)

    // check if we should update UI
    if let _ = fireDate as? NSDate! {
      if currentDate.compare(firedDate) == NSComparisonResult.OrderedDescending {
            loadView3()
      }
    }
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 使用 NSTimer 调用函数,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/38493304/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/38493304/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 使用 NSTimer 调用函数