I want automatic shutdown when timer ends.
I have a timer built here:
class Timer{
var timer = NSTimer();
var handler: (Int) -> ();
let duration: Int;
var elapsedTime: Int = 0;
var targetController = WaitingRoomController.self
init(duration: Int , handler : (Int) -> ()){
self.duration = duration;
self.handler = handler;
}
func start(){
self.timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "onTick", userInfo: nil, repeats: true);
}
func stop(){
println("timer was invaidated from stop()")
timer.invalidate();
}
@objc func onTick() {
self.elapsedTime++;
self.handler(elapsedTime);
if self.elapsedTime == self.duration {
self.stop();
}
}
deinit{
println("timer was invalidated from deinit()")
self.timer.invalidate();
}
}
This is the view controller I want to execute, and how the timer starts:
class WaitingRoomController: UIViewController {
private func handleSetAction(someTime: String){
countDownLabel.text = setTime;
let duration = Utils.getTotalDurationInSeconds(someTime);
timer = Timer(duration: duration ){
(elapsedTime: Int) -> () in
println("handler called")
let difference = duration - elapsedTime;
self.countDownLabel.text = Utils.getDurationInMinutesAndSeconds(difference)
}
timer.start();
}
}
I know the auto segue code:
func doSegue(){
self.performSegueWithIdentifier("asdf", sender: self)
}
but I don’t know how to connect the timer and this function together.
source
share