2

ファイルのアップロード中にアクティビティインジケーターを作成しようとしているので、多くの解決策を見つけましたが、それらを完全には理解していないと思うので、コードは次のようになります。

- (void) startSpinner {

UIActivityIndicatorView  *spinner = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[spinner setCenter:CGPointMake(self.view.frame.size.width/2.0, self.view.frame.size.height/2.0)];
[self.view addSubview:spinner];
[spinner startAnimating];

}


- (void)startSync {

[NSThread detachNewThreadSelector:@selector(startSpinner) toTarget:self withObject:nil];

// computations 

[self.spinner stopAnimating];
} 

そのため、[self startSync] activityIndi​​cator を実行すると、表示されますが、アップロードしても停止しませんでした。また、viewDidLoad などの別の場所 ((void)startSpinner ではなく) でアクティビティ インジケーターを宣言し、[self startAnimating] のみを実行すると、まったく表示されませんでした。エラーを見つけるのを手伝ってください。

4

3 に答える 3

4

You're performing UI operations on a thread that isn't the main thread. You should never call detachNewThreadSelector with a selector that performs UI related tasks.

A better, more understandable way to do this is:

dispatch_async(dispatch_get_main_queue(), ^{
    [self.activityIndicator startAnimating];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        //Perform lengthy operations
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.activityIndicator stopAnimating];
        });
    });
});

Also, if you choose to work with selectors - make sure your UIActivityIndicatorView is declared outside the scope of the method.

于 2013-02-05T08:07:27.727 に答える
1
- (void) startSpinner
{
    self.spinner = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    [spinner setCenter:CGPointMake(self.view.frame.size.width/2.0, self.view.frame.size.height/2.0)];
    [self.view addSubview:spinner];
    [spinner startAnimating];
}


- (void)startSync
{
    [NSThread detachNewThreadSelector:@selector(startSpinner) toTarget:self withObject:nil];

    // computations 

    [self.spinner performSelectorOnMainThread:@selector(stopAnimating) withObject:nil waitUntilDone:NO];
    self.spinner = nil;
} 
于 2013-02-05T08:07:00.880 に答える
1

It happens become you declare local variable spinner inside startSpinner method.

When you call self.spinner, it does not affect the local variable spinner you declared inside startSpinner method. You have 2 separate variable with same name.

You have to declare

spinner = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];

于 2013-02-05T08:08:31.780 に答える