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 02-13-2010, 06:41 PM   #1 (permalink)
Registered Member
 
Join Date: Nov 2009
Location: Orange County, California
Posts: 36
ShayansMind is on a distinguished road
Send a message via AIM to ShayansMind Send a message via Skype™ to ShayansMind
Smile NSTimer invalidate at a certain time!

Hi everybody,

So I'm coding an app where there is a timer that counts down from 15 seconds.. I want the app to invalidate the timer at 0 seconds and display an actionsheet telling the time. I have the code for the actionsheet and everything else, but I can't figure out the code for the timer to invalidate at 0 seconds.

Here is my code

-(void)randomMainVoid {

mainInt --;
time.text = [NSString stringWithFormat:@"Time: %d", mainInt];
}

-(void)viewDidLoad {

mainInt = 15;
randomMain = [NSTimer scheduledTimerWithTimeInterval-1.0/-1.0) target:self selector:@selector(randomMainVoid) userInfo:nil repeats:YES];

}

the timer counts DOWN from 15 but then goes to the negatives I want it to stop at 0 how do I do that?

Thanks for anyhelp
ShayansMind is offline   Reply With Quote
Old 02-13-2010, 06:51 PM   #2 (permalink)
iPhone SDK learner
 
Join Date: Feb 2010
Location: Illinois, USA
Posts: 421
Batman is on a distinguished road
Default

i would use:
Code:
 if (mainInt > 0) {
mainInt--;
time.text = [NSString stringWithFormat:@"Time: %d", mainInt];
}
Batman is offline   Reply With Quote
Old 02-13-2010, 06:55 PM   #3 (permalink)
Obj-C Learner
 
Join Date: Apr 2009
Location: Manchester, UK
Posts: 1,030
ZunePod is on a distinguished road
Send a message via MSN to ZunePod Send a message via Yahoo to ZunePod
Default

Quote:
Originally Posted by ShayansMind View Post
Hi everybody,

So I'm coding an app where there is a timer that counts down from 15 seconds.. I want the app to invalidate the timer at 0 seconds and display an actionsheet telling the time. I have the code for the actionsheet and everything else, but I can't figure out the code for the timer to invalidate at 0 seconds.

Here is my code

Code:
-(void)randomMainVoid {
	if(mainInt >0){
	mainInt --;
	time.text = [NSString stringWithFormat:@"Time: %d", mainInt];
        }
        if(mainInt == 0){
        [randomMain invalidate];
        }
}	

-(void)viewDidLoad {

mainInt = 15;
	randomMain = [NSTimer scheduledTimerWithTimeInterval:(-1.0/-1.0) target:self selector:@selector(randomMainVoid) userInfo:nil repeats:YES];	

}
the timer counts DOWN from 15 but then goes to the negatives I want it to stop at 0 how do I do that?

Thanks for anyhelp
That should work, although you should use else, instead of the second if, but i cba writing it.
ZunePod is offline   Reply With Quote
Old 02-13-2010, 07:18 PM   #4 (permalink)
Registered Member
 
Join Date: Nov 2009
Location: Orange County, California
Posts: 36
ShayansMind is on a distinguished road
Send a message via AIM to ShayansMind Send a message via Skype™ to ShayansMind
Smile NSTimer inva...

Thanks everyone for the codes, I will try them out and if they work I will post back
ShayansMind is offline   Reply With Quote
Old 02-13-2010, 07:30 PM   #5 (permalink)
Registered Member
 
Join Date: Nov 2009
Location: Orange County, California
Posts: 36
ShayansMind is on a distinguished road
Send a message via AIM to ShayansMind Send a message via Skype™ to ShayansMind
Default THANKS!

Quote:
Originally Posted by ZunePod View Post
That should work, although you should use else, instead of the second if, but i cba writing it.
Thanks the code works and I implemented my actionsheet and it works too! Thanks so much, I used your code not the above 1
ShayansMind is offline   Reply With Quote
Old 02-13-2010, 07:34 PM   #6 (permalink)
Obj-C Learner
 
Join Date: Apr 2009
Location: Manchester, UK
Posts: 1,030
ZunePod is on a distinguished road
Send a message via MSN to ZunePod Send a message via Yahoo to ZunePod
Default

Well it is the same code apart from the second bit.

Thanks anyway.
ZunePod is offline   Reply With Quote
Reply

Bookmarks

Tags
code, integer, label, nstimer, update

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: 332
5 members and 327 guests
2Apps1Day, akacaj, SLIC, soohyun, Techgirl-52
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,650
Threads: 94,115
Posts: 402,887
Top Poster: BrianSlick (7,990)
Welcome to our newest member, soohyun
Powered by vBadvanced CMPS v3.1.0

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