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

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

    最初我得解決方案是在主控制器文件中得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并在應用程序恢復活動后創建新得計時器?所遇到得程序開發問題。

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

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

    返回頂部

    主站蜘蛛池模板: 日韩精品一区二区三区不卡 | 久久99国产一区二区三区| 精品无码人妻一区二区三区| 亚洲人成人一区二区三区| 亚洲成在人天堂一区二区| 国产成人精品一区二区秒拍 | 亚洲日本一区二区三区| 日韩人妻无码一区二区三区久久| 波多野结衣中文一区| 亚洲av无码成人影院一区| 久久精品国产一区二区三区不卡| 国产高清在线精品一区二区| 日本一区中文字幕日本一二三区视频 | 不卡无码人妻一区三区音频| 国产精品va一区二区三区| 福利片免费一区二区三区| 狠狠色婷婷久久一区二区三区| 久久精品一区二区三区四区| 中文字幕在线一区二区在线 | 无码人妻AV免费一区二区三区| 搡老熟女老女人一区二区| 亚洲另类无码一区二区三区| 成人在线一区二区| 日韩精品无码一区二区三区不卡 | 亚洲国产韩国一区二区| 国产伦精品一区二区三区免费迷 | 无码人妻久久一区二区三区免费| 日本一区免费电影| 国产精品 视频一区 二区三区| 国产一区二区三区在线| 动漫精品专区一区二区三区不卡 | 国产情侣一区二区三区| 美女视频免费看一区二区 | 制服中文字幕一区二区| 国产成人精品一区二区秒拍| 亚洲一区二区影视| 91精品一区国产高清在线| 日韩免费无码一区二区三区| 国产一区高清视频| 波多野结衣一区二区| 国产一区三区二区中文在线|