0

アクティビティインジケーターを機能させることができません。

これが私が持っているものです-

-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:YES];
//Create an instance of activity indicator view
UIActivityIndicatorView * activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
//set the initial property
[activityIndicator stopAnimating];
[activityIndicator hidesWhenStopped];
//Create an instance of Bar button item with custome view which is of activity indicator
UIBarButtonItem * barButton = [[UIBarButtonItem alloc] initWithCustomView:activityIndicator];
//Set the bar button the navigation bar
[self navigationItem].rightBarButtonItem = barButton;
//Memory clean up
[activityIndicator release];
[barButton release];
}

それを開始して終了することになっているコードの部分--

...
    else if ([theSelection isEqualToString: @"Update statistics"])
    {
        [self startTheAnimation];
        [updateStatistics  updateThe2010Statistics];
        [self stopTheAnimation];
    }
...


-(void)startTheAnimation {
    [(UIActivityIndicatorView *)[self navigationItem].rightBarButtonItem.customView startAnimating];
}

-(void)stopTheAnimation {
    [(UIActivityIndicatorView *)[self navigationItem].rightBarButtonItem.customView stopAnimating];
}
4

2 に答える 2

0

[updateStatistics updateThe2010Statistics];ほとんどの場合、システムのイベント スレッドのブロックに悩まされています。 IBAction コールバックまたはシステムによってトリガーされる他のメソッド ( など) から-viewDidLoad呼び出しているメソッドを実行しています-viewWillAppearか?

その場合、実行時間の長いタスクがイベント スレッドをブロックし、アクティビティ インジケーターを更新できなくなります。次のことを試してください。

...
else if ([theSelection isEqualToString: @"Update statistics"])
{
  [self startTheAnimation];
  [self performSelectorInBackground:@selector(doUpdateStatistics) withObject:nil];
}
...

- (void) doUpdateStatistics {
  [updateStatistics  updateThe2010Statistics];
  [self performSelectorOnMainThread:@selector(stopTheAnimation) withObject:nil waitUntilDone:NO];
}

これにより、2 番目のスレッドで統計の更新が実行され、イベント スレッドがアクティビティ インジケーターを適切に更新できるようになります。統計の更新の最後に、アクティビティ インジケーターを停止するために、メイン スレッド (つまり、イベント スレッド) で停止アニメーションを再度呼び出します。

于 2010-07-23T17:45:43.090 に答える
0

少なくとも次のように変更します。

   [activityIndicator hidesWhenStopped];

に:

   activityIndicator.hidesWhenStopped = YES;

または、YES がデフォルトであるため、その行を削除します。

于 2010-07-23T17:30:20.923 に答える