Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

How do i stop a timer in another ViewController (SWIFT)

If i started a ViewController1 -
let timer2 = NSTimer.scheduledTimerWithTimeInterval(10, target: self, selector: "timerFinished:", userInfo: nil, repeats: false)

How do I then stop that same timer when a button is pressed in another view controller? (language is Swift)

You can use notifications.

//Put this in the view controller that has the button

NSNotificationCenter.defaultCenter().postNotificationName("StopTimerNotification", object: nil)

//Put this in the view controller with the timer

NSNotificationCenter.defaultCenter().addObserver(self, selector: "StopTimerNotication:", name:"StopTimerNotification", object: nil)

//Method of received Notification

func StopTimerNotication(notification: NSNotification){ //Put stop timer code here.

}

It put timer.invalidate() here

func StopTimerNotication(notification: NSNotification){ //Put stop timer code here.

timer.invalidate() }

I get the error "use of unresolved identifier"

5 Answers

Is this the view controller where you declared your timer? Also, if you cut and paste this code try taking the comment out. It's not marked up properly and that may be causing some trouble.

This is in a different View Controller, I have no timers declared in this one. I took out the comments but I still get the error.

Now i get an error for

import UIKit "Declaration only valid at file scope"

which was added by Xcode and it didn't come up when I ran my app before.

You can't access a timer declared in a different view controller. The pattern described in the first comment should work. This code needs to be in the view controller where you declared the timer and started the timer.

func StopTimerNotication(notification: NSNotification){

}

I'm trying to use the timer to stop a game. When the timer is done i want it to go to ViewController1 (end game View). If the user gets and answer wrong in the game (game is separate Views) the game will end so the timer won't be needed thus it needs to be stopped.

PS: The game itself has many Views so I was going to use the start game button which is in ViewController3(to start the timer) and the Back button in the end screen (View Controller 1) to stop the timer.

The timer cannot be stopped outside of the scope in which it is declared. However, you can send a message ( a notification) from another view controller back to the timer view controller to achieve the result you are looking for. So, in your scenario, you write: if the user gets an answer wrong the game will end and the timer won't be needed. So, it is from this view controller you will send a notification to stop the timer.

NSNotificationCenter.defaultCenter().postNotificationName("StopTimerNotification", object: nil)

However, you also have to tell the view controller where the timer is declared to listen for this type of notification which we named StopTimerNotification and what to do when it receives it. Note: Put this in your viewDidLoad method.

NSNotificationCenter.defaultCenter().addObserver(self, selector: "StopTimerNoticationFunction:", name:"StopTimerNotification", object: nil)

Finally, you need to implement the code to stop the timer. In this example, we have named this callback StopTimerNotificationFunction. So. put this in your file where you have declared the timer.

func StopTimerNotication(notification: NSNotification){ 

//Put stop timer code here.

}

How do I show the End Game View when the timer is done