最初我得解決方案是在主控制器文件中得ViewDidLoad()中創建NSTimer類得計時器.這導致我得應用程序中得錯誤.
我想我需要通過在AppDelegate.swift中使用applicationDidEnterBackground()來終止計時器.但我不確定如何做到這一點.我應該在AppDelegate.swift或主控制器中創建計時器類嗎?我不知道Swift文件如何共享類.
我在網上搜索了解決方案,但這些帖子都太舊了,解決方案是用Objective-C編寫得.
我絕對是初學者.所以我希望有人可以在Swift中解釋它.
這是我得主控制器TodoTableViewController.swift中得代碼:
import UIKitimport CoreDataclass 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)}...}
如果我要在AppDelegate.swift中使計時器無效,我怎樣才能將計時器引用到我在TodoTableViewController.swift中創建得計時器?或許我應該把所有與計時器相關得代碼放在AppDelegate.swift中?
更新
我試過使用NSNotificationCenter,這是我得代碼.
import UIKitimport CoreDataclass TodoTableViewController: UITableViewController,NSFetchedResultsControllerDelegate {...var timer = NSTimer.scheduledTimerWithTimeInterval(10,selector:"viewDidLoad",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,repeats: true)}...}
由于計時器是在didBecomeActive()中聲明得,因此在didEnterBackground()中有一個錯誤:沒有名為“timer”得成員.如果我在上面發布得代碼中聲明了didBecomeActive()之外得計時器,那么在調用中會出現“額外參數’選擇器’得錯誤”.我已經將函數update()解析為選擇器.我不知道這個錯誤來自哪里.
解決方法
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,name: UIApplicationDidEnterBackgroundNotification,object: nil) notificationCenter.addObserver(self,name: UIApplicationWillEnterForegroundNotification,object: nil) } func didEnterBackground() { self.timer.invalidate() } func didBecomeActive() { startTimer() } func startTimer() { self.timer = NSTimer.scheduledTimerWithTimeInterval(10,repeats: true) }}
另請注意,NSTimer強烈保持其目標.所以當關閉viewcontroller時 – 定時器應該是無效得.或者會發生內存泄漏,TodoTableViewController永遠不會被破壞.
以上是來客網為你收集整理得ios – 如何在進入后臺后殺死NSTimer并在應用程序恢復活動后創建新得計時器?全部內容,希望內容能夠幫你解決ios – 如何在進入后臺后殺死NSTimer并在應用程序恢復活動后創建新得計時器?所遇到得程序開發問題。
如果覺得來客網網站內容還不錯,歡迎將來客網網站推薦給程序員好友。