Problem creating a timer in Objective C for the Iphone
I've been having alot of problems creating a timer for a iphone application, currently I have a label displaying my timer, but it is just displaying the variable i (2700) in the label, and not decreasing by one every second. Its really driving my head in as this is really my 1st attempt coding in obj C, and for the life of me I cannot get it to decrease in the label.
I was hoping one of the programs on this site would be able to readover my code (MainViewController.m), with particular reference to the timer (countDown and viewDidLoad) and help me fix my problem.
Cheers in advance.
#import "MainViewController.h"
#include <unistd.h>
#include <stdio.h>
int i = 2700;
@implementation MainViewController
@synthesize window;
@synthesize InformationAboutRSI, mainView;
- (IBAction) InformationAboutRSI {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.rsi.org.au/what-is-rsi.html"]];
}
-(void)countDown {
if (i > 0) {
i--;
countdownLa开发者_JAVA百科bel.text = [NSString stringWithFormat:@"%i",i];
sleep(100);
}
}
- (void)viewDidLoad {
int countDown();
[countdownLabel setFont:[UIFont fontWithName:@"DBLCDTempBlack" size:128.0]];
countdownLabel.text = [NSString stringWithFormat:@"%i",i];
[super viewDidLoad];
}
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller {
[self dismissModalViewControllerAnimated:YES];
}
- (IBAction)showInfo:(id)sender {
FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@"FlipsideView" bundle:nil];
controller.delegate = self;
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)dealloc {
[window release];
[super dealloc];
}
-(void)updateLabel {
countdownLabel.text = [NSString stringWithFormat:@"%i",i];
}
@end
When developing applications for most modern operating systems, you generally don't want to block the UI thread. This means that your code should do its work and get out of the way as quickly as possible, because while it blocks, the application can't do anything else (like respond to button presses). On a desktop application, blocking the UI thread for more than a couple of seconds will display an hourglass or pinwheel cursor.
With that in mind, you could design your countDown method to:
- Decrement the counter
- Update the label
Create an NSTimer (I would try scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:) in your initialization to ask the operating system to call countDown every X seconds.
You are calling your countdown method only once, i don't see a loop or timer that would repeat this action.
Have a look at the NSTimer reference.
or use a classic for loop
-(void)countDown
{
for (int j = i, j > 0, j--)
{
countdownLabel.text = [NSString stringWithFormat:@"%i",j];
sleep(100);
}
}
you could call this method after loading your view and initializing the textfield with [self countDown];
You need to return from your countDown method without sleeping and without setting the label more than once. The display will only update after you exit your code.
Then you need to find a way to call your countDown method again after a long enough delay and without sleeping anywhere else. (Hint: use NSTimer) Rinse and repeat.
精彩评论