0

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).

4

2 回答 2

1

一些事情:

  • [super viewDidLoad];最好排在第一位viewDidLoad
  • 每次执行时无需设置字体timerVoid
  • 正如我在评论中提到的,你应该打电话invalidatetimer不是打电话mainInt
  • 滑块不会修改mainInt- 您已将 mainInt 的值设置为保持滑块的初始值,并且当您更改滑块的值时它不会自行更改。为此,您应该创建一个 IBAction 并将其连接到滑块的 valueChanged 事件。在该 IBAction 中,您可以使用滑块的新值做任何您想做的事情 - 例如设置 mainInt 的值或重新安排您的计时器。
  • mySlider.value您也可以通过在任何需要的地方直接使用 IBAction 来避免使用 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];

于 2010-08-28T10:19:14.953 回答
0

您的应用程序正在崩溃,因为您尚未将滑块中的 valueChanged 事件链接到任何正确的 IBAction 方法。

于 2010-08-28T18:45:12.970 回答