我在创建可用于修改我创建的NSTimer的UISlider时遇到了问题。基本上滑块是修改NSTimer倒计时的整数,但当我尝试移动UISlider时,应用程序崩溃,我假设这是因为它干扰了正在发生的倒计时,但我不知道我不知道如何解决这个问题。
以下是相关代码
- (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];
}
滑块名为mySlider,它正在修改整数“mainInt”(第5行)。
答案 0 :(得分:1)
少数事情:
[super viewDidLoad];
行最好是viewDidLoad
中的第一个。timerVoid
时都无需设置字体。invalidate
而不是timer
上致电mainInt
。mainInt
- 您已将mainInt的值设置为保持滑块的初始值,并且当您更改滑块的值时它不会自行更改滑块。为此,您应该创建一个IBAction并将其连接到slider的valueChanged事件。在IBAction中,您可以使用滑块的新值执行任何操作 - 例如,设置mainInt的值或重新计划您的计时器。mySlider.value
来避免使用IBAction。可能的代码:
- (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];
}
修改强>:
添加了[alert release];
行
答案 1 :(得分:0)
您的应用程序崩溃了,因为您尚未将滑块中的valueChanged事件与任何正确的IBAction方法相关联。