开发者

Can’t get a spinner to appear

I would like to use a spinner. But, this code below does not display a spinner and I'm not sure why. How to make this work? BTW, It is being called from a submit button I created.

//spinner declared in .h file
UIActivityIndicatorView   *aSpinner; 

//throw up spinner from submit btn we created
aSpinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:
    UIActivityIndicatorViewStyleWhiteLarge];

[self.view addSubview:aSpinner]; 
[aSpinner release]; 
[aSpinner startAnimating]; 

//send blocking request 
[request startSynchronous];

//get rid of spinner when finished delegate is fired
- (v开发者_如何学编程oid)requestFinished:(ASIHTTPRequest *)request 
{ 
    NSLog(@"REQUEST FINISHED"); 
    [aSpinner stopAnimating]; 
    //[aSpinner release]; 
} 


If you call some blocking code immediately after you display the spinner, the UI won’t get updated, since it only updates when the main run loop is running. If this really is the source of the problem, the spinner should show up when you comment out the [request startSynchronous] line for a test.

The solution would be to use an asynchronous request. The delegating code looks like you already do that, but on the other hand the start call mentions synchronous operation. Care to explain? (Or did I overlook something?)


//spinner declared in .h file
UIActivityIndicatorView  *aSpinner; 

Add a property in the header file as-well:

@property (nonatomic, retain) UIActivityIndicatorView *aSpinner;

Don't forget to synthesize in .m file!

//throw up spinner from submit btn we created
UIActivityIndicatorView *tempSpinner = [[UIActivityIndicatorView alloc]  initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
self.aSpinner = tempSpinner;
[tempSpinner release];

[self.view addSubview:self.aSpinner]; 
[self.aSpinner startAnimating]; 

//send blocking request 
[request startSynchronous];


//get rid of spinner when finished delegate is fire
- (void)requestFinished:(ASIHTTPRequest *)request { 
      NSLog(@"REQUEST FINISHED");
      [self.aSpinner stopAnimating]; 
}

In your dealloc method you write: [aSpinner release]; This is however just one of many approaches.


The problem could be in the view that you're adding the spinner to. Is is capable of, and does it have the dimensions to display the activity indicator ? (e.g. UIBarButtonItems cannot handle addSubview)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜