1

こんにちは、iPhone アプリでカスタム プログレス バーを表示しようとしています。プログレス バーの値をインクリメントするメソッドを作成しています。値が 100% になったら、タイマーを無効にする必要があり、この再帰を停止する必要があります。次のviewControllerを表示します。私のコードスニペットは以下のようなものです、

-(void)progressNextValue
{
    progressValue += 1.0f;
    if(progressValue >= progress.maxValue){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"end" message:@"TimeOut!!!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
        NSLog(@"Time Out!!!!");
        [mytimer invalidate];
        Second *sec = [[Second alloc] initWithNibName:@"Second" bundle:nil];
        [self.view addSubview:sec.view];    
    }

    progress.currentValue = progressValue;
    mytimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(progressNextValue) userInfo:nil repeats:YES];

}

- (void)viewDidLoad
{
    [super viewDidLoad];
    progress.maxValue = 100.0f;    
    [self progressNextValue];
}

ここでも、私progressValue = progress.maxValuemytimerが無効化されていません。

どんな助けでも事前に感謝します。

4

4 に答える 4

2

メソッドが実行されるたびにタイマーを設定しています。return ステートメントを追加するか、タイマーのインスタンス化を else ステートメントに配置します。

例えば:

-(void)progressNextValue
{
    progressValue += 1.0f;
    if(progressValue >= progress.maxValue){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"end" message:@"TimeOut!!!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
        NSLog(@"Time Out!!!!");
        [mytimer invalidate];
        Second *sec = [[Second alloc] initWithNibName:@"Second" bundle:nil];
        [self.view addSubview:sec.view];    
    } else {
        // Move this line inside the else statements so that it only gets run if 
        // the progress bar is not full.
        mytimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(progressNextValue) userInfo:nil repeats:YES];
    }

    progress.currentValue = progressValue;
}
于 2012-07-11T13:20:42.677 に答える
2

このコードは問題を引き起こします、

mytimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(progressNextValue) userInfo:nil repeats:YES];

ここで、 repeatを使用してタイマーを作成するたびに、それが問題になります。

progressNextValue他のメソッドからメソッドを呼び出します。

-(void)tempMethod

    mytimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(progressNextValue) userInfo:nil repeats:YES];
}

または、次から呼び出すだけです。

- (void)viewDidLoad
{
    [super viewDidLoad];
    progress.maxValue = 100.0f;    
    mytimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(progressNextValue) userInfo:nil repeats:YES];
}
于 2012-07-11T13:15:04.313 に答える
0

タイマーを無効にするには、 を呼び出す必要があります[myTimer invalidate]。あなたがすること。しかしmyTimer、あなたの場合、私が見る限り保持されていません。したがってretain、それを割り当てるreleaseときと無効にするときです。

お役に立てれば。

乾杯!

于 2012-07-11T13:19:27.747 に答える
0

内でタイマーを呼び出す代わりにprogressNextValueviewDidLoad(または開始したい他の場所で)呼び出します。タイマーへの参照を保持し(ページの上部に置くNSTimer *t)、条件が満たされたときに実行し[t invalidate]ます。

問題は、「progressNextValue」内でタイマーを呼び出すと、繰り返すように指示しているため、そこでタイマーを無効にしてもあまり効果がないことです (複数のタイマーが実行されているため)。

于 2012-07-11T13:20:15.763 に答える