Ant*_*ony 6 iphone timer nstimer ios swift
所以我正在使用Swift构建一个简单的iOS应用程序.我需要NSTimer在应用程序进入后台后杀死我,并在应用程序再次激活后创建一个新应用程序.
最初我的解决方案是在主控制器文件中创建NSTimerclass in 的计时器ViewDidLoad().这导致我的应用程序中的错误.
我想我需要applicationDidEnterBackground()在AppDelegate.swift中使用来杀死计时器.但我不确定如何做到这一点.我应该在AppDelegate.swift或主控制器中创建计时器类吗?我不知道Swift文件如何共享类.
我在网上搜索了解决方案,但这些帖子都太旧了,解决方案是用Objective-C编写的.
我绝对是初学者.所以我希望有人可以在Swift中解释它.
这是我的主控制器TodoTableViewController.swift中的代码:
import UIKit
import CoreData
class TodoTableViewController: UITableViewController, NSFetchedResultsControllerDelegate {....
...
...
override func viewDidLoad() {
super.viewDidLoad()
var fetchRequest = NSFetchRequest(entityName: "Todo")
let sortDescriptor = NSSortDescriptor(key: "dayleft", ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor]
if let managedObjectContext = (UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext {
fetchResultController = NSFetchedResultsController(fetchRequest:fetchRequest, managedObjectContext: managedObjectContext, sectionNameKeyPath: nil, cacheName: nil)
fetchResultController.delegate = self
var e: NSError?
var result = fetchResultController.performFetch(&e)
todos = fetchResultController.fetchedObjects as [Todo]
if result != true {
println(e?.localizedDescription)
} }
var timer = NSTimer.scheduledTimerWithTimeInterval(10, target: self, selector:"update", userInfo:nil, repeats: true)
}
...
}
Run Code Online (Sandbox Code Playgroud)
如果我要在AppDelegate.swift 中使计时器无效,我怎样才能将计时器引用到我在TodoTableViewController.swift中创建的计时器?或许我应该把所有与计时器相关的代码放在AppDelegate.swift中?
更新
我试过使用NSNotificationCenter,这是我的代码.
import UIKit
import CoreData
class TodoTableViewController: UITableViewController, NSFetchedResultsControllerDelegate {
...
var timer = NSTimer.scheduledTimerWithTimeInterval(10, target: self, selector:"viewDidLoad", userInfo:nil, repeats: true)
func update(){
.....
}
override func viewDidLoad() {
super.viewDidLoad()
...
let notificationCenter = NSNotificationCenter.defaultCenter()
notificationCenter.addObserver(self, selector: "didEnterBackground", name: "UIApplicationDidEnterBackgroundNotification", object: UIApplication.sharedApplication())
notificationCenter.addObserver(self, selector: "didBecomeActive", name: "UIApplicationWillEnterForegroundNotification", object: UIApplication.sharedApplication())
}
func didEnterBackground() {
timer.invalidate()
}
func didBecomeActive() {
var timer = NSTimer.scheduledTimerWithTimeInterval(10, target: self, selector:"update", userInfo:nil, repeats: true)
}
...
}
Run Code Online (Sandbox Code Playgroud)
由于声明了计时器didBecomeActive(),didEnterBackground()因此存在错误:没有名为"timer"的成员.如果我像上面发布的代码一样在timer外面声明didBecomeActive(),那么在调用中会出现"额外参数'选择器'的错误".我已经将函数解析update()为选择器.我不知道这个错误来自哪里.
Mik*_*ail 10
更新了您的代码并添加了一些注释:
class TodoTableViewController: UITableViewController, NSFetchedResultsControllerDelegate
//better to instantiate timer inside viewDidLoad
var timer: NSTimer!
func update(){ }
override func viewDidLoad() {
super.viewDidLoad()
startTimer()
let notificationCenter = NSNotificationCenter.defaultCenter()
//UIApplicationDidEnterBackgroundNotification & UIApplicationWillEnterForegroundNotification shouldn't be quoted
notificationCenter.addObserver(self, selector: "didEnterBackground", name: UIApplicationDidEnterBackgroundNotification, object: nil)
notificationCenter.addObserver(self, selector: "didBecomeActive", name: UIApplicationWillEnterForegroundNotification, object: nil)
}
func didEnterBackground() {
self.timer.invalidate()
}
func didBecomeActive() {
startTimer()
}
func startTimer() {
self.timer = NSTimer.scheduledTimerWithTimeInterval(10, target: self, selector:"update", userInfo:nil, repeats: true)
}
}
Run Code Online (Sandbox Code Playgroud)
还要注意NSTimer强烈保持它target.所以当关闭viewcontroller时 - 定时器应该是无效的.或者会发生内存泄漏,TodoTableViewController永远不会被破坏.
| 归档时间: |
|
| 查看次数: |
5623 次 |
| 最近记录: |