2016-03-04 50 views
2

我有一个NSTimer()开始于点击按钮。如果我想让timer显示在标签上,我可以在同一个视图控制器中执行此操作,但是我希望定时器继续,并且值(1 ... 2 ... 3)显示在下一个视图控制器。我有一个标签,它在下一个视图控制器上显示时间变量,但它只显示时间变量的值,当按钮被按下并且不会继续时。时间变量被传递,但运行它的函数不是。我该怎么做呢?如何使用Swift在ViewControllers中继续使用函数?

var timer = NSTimer() 

var time = 0 

内部viewDidLoad中:

timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("timerFunc"), userInfo: nil, repeats: true) 

    func timerFunc() { 

     time++ 

//time label displays the time (1..2...3 etc) 

     timeLabel.text = String(time) 

    } 

SecondSceneViewController具有从这个视图控制器通过一个时间变量:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
      var secondScene = segue.destinationViewController as!   
           SecondViewController 

      secondScene.time = time 
    } 

当我去secondViewController,时间变量内的值是什么时候变量是当按下按钮时并且它不会继续运行。我将如何去计时器传递到下一个视图控制器来显示值?

回答

4

IMO应提取定时器的代码为Singleton,然后从两个ViewControllers访问

这里有一个简单的让你开始:

class TimerManager { 

var realSharedInstance: TimerManager? 
var sharedInstance: TimerManager { 
    get{ 
     if let realSharedInstance = realSharedInstance { 
      return realSharedInstance 
     } 
     else{ 
      realSharedInstance = TimerManager() 
      return realSharedInstance! 
     } 
    } 
} 

var timer: NSTimer 

init() { 
    timer = NSTimer() 
} 

func rest() { 
    timer = NSTimer() 
} 

}

0

如果您保留这两个变量

var timer = NSTimer() 
var time = 0 

外面th他们成为全球性的,你可以从另一个视图控制器访问它们。

这样的例子:

import UIKit 
import CoreData 

var fromJSON = true 

// Retreive the managedObjectContext from AppDelegate 
let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext 

class CoreDataController: UIViewController { 

另一种选择是用一个通知中心工作:

NSNotificationCenter.defaultCenter().addObserver(
    self, 
    selector: "batteryLevelChanged:", 
    name: UIDeviceBatteryLevelDidChangeNotification, 
    object: nil) 

@objc func batteryLevelChanged(notification: NSNotification){  
    //do stuff 
} 
3

我会创建一个单将处理计时器事件。

let myTimerNotificationNameKey = "timerKey" 
    let myTimerNotificationUserInfoTimeKey = "timerUserInfoKey" 

    class myTimer { 
     static let sharedInstance = myTimer() 
     var timer: NSTimer 
     var time = 0 

     func startTimer() { 
     time = 0 
     timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "timerFunc:", userInfo: nil, repeats: true) 

     } 

     func stopTimer() { 
     timer.invalidate() 
     timer = nil 
     } 

     func timerFunc(timer: NSTimer) { 
      NSNotificationCenter.defaultCenter().postNotificationName(myTimerNotificationNameKey,object: nil, userInfo: {myTimerNotificationUserInfoTimeKey: time++}) 

     } 
    } 

在第一个视图控制器调用myTimer.sharedInstance.startTimer()功能,将启动定时器。但首先要记住听通知。通知将收到带有时间计数的用户信息字典。在第二个视图控制器上做同样的事情。

现在你有了一个可以监听多个视图控制器的计数器。记住停止计时器,一旦你停止需要它。

注:我没有编译过这段代码。

+0

@ carlos-e-hernandez你会如何继续?你会在哪里启动myTimer类? – boehmatron

+0

@boehmatron myTimer类可以使用'sharedInstance'静态方法来访问,它使这个类成为一个单独的类。您可以随处访问它。不是最好的设计,但解决了OP的问题。另一种解决方案是使用依赖注入,拥有一个类自己的myTimer,然后在需要访问它的每个视图控制器上注入myTimer。 – carlos