0

作成した 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 行目)。

4

2 に答える 2

1

いくつかのこと:

  • [super viewDidLoad];行は先頭にある方が良いですviewDidLoad
  • 実行するたびにフォントを設定する必要はありませんtimerVoid
  • コメントで述べたように、 ではなく に電話する必要がありinvalidateます。timermainInt
  • スライダーは変更されませんmainInt- スライダーの初期値を保持するように mainInt の値を設定しましたが、スライダーの値を変更してもそれ自体は変更されません。そのためには、IBAction を作成し、それをスライダーの valueChanged イベントに接続する必要があります。その IBAction 内では、スライダーの新しい値を使用して、必要なことを実行できます。たとえば、mainInt の値を設定したり、タイマーを再スケジュールしたりします。
  • また、必要な場所で直接使用することにより、IBAction の使用を避けることもmySlider.valueできます。
  • アプリがクラッシュする理由はわかりませんが...

可能なコード:

- (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 に答える