ios – 如何在進入后臺后殺死NSTimer并在應用程序恢復活動后創(chuàng)建新得計時器?

    所以我正在使用 Swift構建一個簡單得iOS應用程序.我需要在應用程序進入后臺后終止我得NSTimer,并在應用程序再次激活后創(chuàng)建一個新得NSTimer.

    最初我得解決方案是在主控制器文件中得ViewDidLoad()中創(chuàng)建NSTimer類得計時器.這導致我得應用程序中得錯誤.

    我想我需要通過在AppDelegate.swift中使用applicationDidEnterBackground()來終止計時器.但我不確定如何做到這一點.我應該在AppDelegate.swift或主控制器中創(chuàng)建計時器類嗎?我不知道Swift文件如何共享類.

    我在網(wǎng)上搜索了解決方案,但這些帖子都太舊了,解決方案是用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中創(chuàng)建得計時器?或許我應該把所有與計時器相關得代碼放在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”得成員.如果我在上面發(fā)布得代碼中聲明了didBecomeActive()之外得計時器,那么在調用中會出現(xiàn)“額外參數(shù)’選擇器’得錯誤”.我已經(jīng)將函數(shù)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時 – 定時器應該是無效得.或者會發(fā)生內存泄漏,TodoTableViewController永遠不會被破壞.

    以上是來客網(wǎng)為你收集整理得ios – 如何在進入后臺后殺死NSTimer并在應用程序恢復活動后創(chuàng)建新得計時器?全部內容,希望內容能夠幫你解決ios – 如何在進入后臺后殺死NSTimer并在應用程序恢復活動后創(chuàng)建新得計時器?所遇到得程序開發(fā)問題。

    如果覺得來客網(wǎng)網(wǎng)站內容還不錯,歡迎將來客網(wǎng)網(wǎng)站推薦給程序員好友。

    聲明:所有內容來自互聯(lián)網(wǎng)搜索結果,不保證100%準確性,僅供參考。如若本站內容侵犯了原著者的合法權益,可聯(lián)系我們進行處理。
    發(fā)表評論
    更多 網(wǎng)友評論1 條評論)
    暫無評論

    返回頂部

    主站蜘蛛池模板: 一区二区三区日韩精品| 色窝窝无码一区二区三区成人网站 | 国产免费一区二区三区不卡| 亚洲熟妇无码一区二区三区导航| 精品视频午夜一区二区| 亚洲色偷精品一区二区三区| 精品三级AV无码一区| 亚洲国产综合无码一区二区二三区| 亚洲精品色播一区二区| 91精品一区二区三区在线观看| 国产一区二区视频在线观看| 日韩一区二区三区在线观看| 国产在线一区观看| 国产成人精品一区二区三在线观看| 无码国产精品一区二区免费虚拟VR| 日产精品久久久一区二区| 久久精品一区二区三区日韩| 少妇无码AV无码一区| 中文字幕Av一区乱码| 插我一区二区在线观看| 中文字幕在线一区| 国产午夜福利精品一区二区三区| 亚洲AV日韩精品一区二区三区| 视频一区在线免费观看| 日韩精品无码Av一区二区| 欧洲精品一区二区三区在线观看 | 精品人无码一区二区三区 | 美女免费视频一区二区| 无码日韩精品一区二区人妻 | 3d动漫精品一区视频在线观看| 久久精品一区二区三区日韩| 精品视频无码一区二区三区 | 综合久久一区二区三区| 国产丝袜美女一区二区三区 | 国产福利电影一区二区三区,亚洲国模精品一区 | 一区二区三区在线免费| 亚洲啪啪综合AV一区| 亚州日本乱码一区二区三区| 国产日韩一区二区三区| 国模无码视频一区| 国产激情无码一区二区三区|