UISlider to modify NSTimer interval crashing application when moved
I have been having problems creating a UISlider that can be used to modify a NSTimer I created. Essentially the slider is ment to modify the integer that the NSTimer is counting down from, but when I try to move the UISlider, the application crashes, I'm assuming this is because it is interfering with the countdown that is occurring, but I don't know what to do to fix this.
Here is the relevant code
- (void)viewDidLoad {
[label setFont:[UIFont fontWithNa开发者_如何学JAVAme:@"DBLCDTempBlack" size:36.0]];
label.text = @"I0A0IN6";
mainInt = mySlider.value;
timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/1.0) target:self selector:@selector (timerVoid) userInfo:nil repeats:YES];
[super viewDidLoad];
}
- (void)timerVoid {
mainInt += -1;
if (mainInt == 0) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Break Time!"
message:@"Time to take a break, please go to the exorcises page during your break inorder to maximise it"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alert show];
[mainInt invalidate];
}
[label setFont:[UIFont fontWithName:@"DBLCDTempBlack" size:36.0]];
label.text=[NSString stringWithFormat:@"%d" , mainInt];
}
The slider is called mySlider, and it is modifying the interger "mainInt" (line 5).
Few things:
[super viewDidLoad];
line is better be first inviewDidLoad
.- There is no need to set the font each time
timerVoid
is executed. - As I have mentioned in the comment, you should call
invalidate
ontimer
and not onmainInt
. - The slider does not modify
mainInt
- you have set the value of mainInt to hold the initial value of your slider and it is not changed by itself when you change the value of the slider. In order to do that you should create an IBAction and connect it to slider's valueChanged event. Inside that IBAction you may do what ever you want with the new value of the slider - for example set the value of mainInt or reschedule your timer. - You may also avoid using the IBAction by using the
mySlider.value
directly everywhere you need. - I don't see any reason for crashing the app though...
Possible code:
- (void)viewDidLoad {
// This line should be first
[super viewDidLoad];
[label setFont:[UIFont fontWithName:@"DBLCDTempBlack" size:36.0]];
label.text = @"I0A0IN6";
// There is no need in mainInt
//mainInt = mySlider.value;
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerVoid) userInfo:nil repeats:YES];
}
- (void)timerVoid {
// This line is much more readable like this ("-=" instead of "+= -")
mySlider.value -= 1;
// Much better to use "<=" in this case to avoid bugs
if (mySlider.value <= 0) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Break Time!"
message:@"Time to take a break, please go to the exorcises page during your break inorder to maximise it"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alert show];
[alert release]; // Haven't noticed the lack of release here earlier...
// timer and not mainInt
[timer invalidate];
}
// The next line is unnecessary
//[label setFont:[UIFont fontWithName:@"DBLCDTempBlack" size:36.0]];
label.text = [NSString stringWithFormat:@"%d", mySlider.value];
}
EDIT:
Added the [alert release];
line
Your app is crashing because you haven't linked the valueChanged event from your slider to any proper IBAction method.
精彩评论