Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 10-21-2010, 02:15 PM   #1 (permalink)
Velvet Rock Apps
 
Join Date: Sep 2010
Posts: 231
ChrisYates is on a distinguished road
Default Cancelling an NSTimer with button tap?

As you can see from the following code I have a repeat set on an NSTimer, is there anyway for me to stop the repeat or somehow stop the fireMethod1 by the user tapping a button?

Code:
[NSTimer scheduledTimerWithTimeInterval:10.0  target:self selector:@selector(fireMethod1) userInfo:nil repeats:YES];
The FireMethod1 code is:
Code:
NSString *path = [[NSBundle mainBundle] pathForResource:@"StaticSound1" ofType:@"mp3"];
	AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    self.staticSounds = theAudio;
	[staticSounds play];
	[theAudio release];
ChrisYates is offline   Reply With Quote
Old 10-21-2010, 03:09 PM   #2 (permalink)
Banned
 
Join Date: Jul 2009
Posts: 576
Not_Appy_At_All is on a distinguished road
Default

Quote:
Originally Posted by ChrisYates View Post
As you can see from the following code I have a repeat set on an NSTimer, is there anyway for me to stop the repeat or somehow stop the fireMethod1 by the user tapping a button?

Code:
[NSTimer scheduledTimerWithTimeInterval:10.0  target:self selector:@selector(fireMethod1) userInfo:nil repeats:YES];
The FireMethod1 code is:
Code:
NSString *path = [[NSBundle mainBundle] pathForResource:@"StaticSound1" ofType:@"mp3"];
	AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    self.staticSounds = theAudio;
	[staticSounds play];
	[theAudio release];
The way I generally do it is to declare my timer in .h

myTimer = [NSTimer scheduledTimer etc etc....

To stop it I do

Code:
[myTimer invalidate];
myTimer = nil;
If it crashes at times...do an IF stmt to check if the timer is nil...

Code:
if(myTimer != nil)
{ 
    [myTimer invalidate];
    myTimer = nil;
}
Stopping the Timer once it has been scheduled to fire, may not work this way..it will complete that last cycle as it is set and forget...So it will stop the Timer from repeating over and over, but that last cycle that is scheduled will still fire...and then it will stop. That is how I understand it. In other words, it will stop it, but not like a knife cutting through butter unless you are timing it in hundredths of a second.

Hope that helps...

You can also use a int counter to limit the number of times you want it to fire...each time it fires it increments by one until it reaches the number you need to stop the timer.

To stop a sound playing...

Code:
    [staticSounds stop];
    staticSounds = nil;
Again, use in IF stmt to test for nil.

Last edited by Not_Appy_At_All; 10-21-2010 at 03:23 PM.
Not_Appy_At_All is offline   Reply With Quote
Old 10-21-2010, 03:57 PM   #3 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default No need to check for nil

Quote:
Originally Posted by Not_Appy_At_All View Post
The way I generally do it is to declare my timer in .h

myTimer = [NSTimer scheduledTimer etc etc....

To stop it I do

Code:
[myTimer invalidate];
myTimer = nil;
If it crashes at times...do an IF stmt to check if the timer is nil...

Code:
if(myTimer != nil)
{ 
    [myTimer invalidate];
    myTimer = nil;
}
Stopping the Timer once it has been scheduled to fire, may not work this way..it will complete that last cycle as it is set and forget...So it will stop the Timer from repeating over and over, but that last cycle that is scheduled will still fire...and then it will stop. That is how I understand it. In other words, it will stop it, but not like a knife cutting through butter unless you are timing it in hundredths of a second.

Hope that helps...

You can also use a int counter to limit the number of times you want it to fire...each time it fires it increments by one until it reaches the number you need to stop the timer.

To stop a sound playing...

Code:
    [staticSounds stop];
    staticSounds = nil;
Again, use in IF stmt to test for nil.
@Not_Appy_At_All:

Unlike many other languages, you don't need to check objects for nil in Objective C.

This code:

Code:
MyObject* foo = nil;
[foo message];
Will not do anything.

Objective C ignores messages sent to nil objects. It takes getting used to, but it saves a lot of checking.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 10-21-2010, 04:43 PM   #4 (permalink)
Velvet Rock Apps
 
Join Date: Sep 2010
Posts: 231
ChrisYates is on a distinguished road
Default

Thanks both for your time, I'll play around with your advice and see if I can get it to work.
ChrisYates is offline   Reply With Quote
Old 10-21-2010, 04:46 PM   #5 (permalink)
Banned
 
Join Date: Jul 2009
Posts: 576
Not_Appy_At_All is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
@Not_Appy_At_All:

Unlike many other languages, you don't need to check objects for nil in Objective C.

This code:

Code:
MyObject* foo = nil;
[foo message];
Will not do anything.

Objective C ignores messages sent to nil objects. It takes getting used to, but it saves a lot of checking.
Thanks Duncan, I appreciate the info.

I don't have the depth of knowledge you do, and wish I did.

That will certainly save a lot of typing.
Not_Appy_At_All is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 347
10 members and 337 guests
condor304, Desert Diva, Domele, dre, mottdog, oceanlablight, palme2elie, Paul Slocum, schmallegory
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,658
Threads: 94,118
Posts: 402,895
Top Poster: BrianSlick (7,990)
Welcome to our newest member, palme2elie
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:14 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0