Reputation: 1
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 fontWithName:@"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).
Upvotes: 0
Views: 714
Reputation: 70733
Your app is crashing because you haven't linked the valueChanged event from your slider to any proper IBAction method.
Upvotes: 0
Reputation: 14235
Few things:
[super viewDidLoad];
line is better be first in viewDidLoad
.timerVoid
is executed.invalidate
on timer
and not on mainInt
.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.mySlider.value
directly everywhere you need.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
Upvotes: 1