Can’t get a spinner to appear

14,444

Solution 1

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?)

Solution 2

//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.

Share:
14,444
iPhone Developer
Author by

iPhone Developer

Updated on June 29, 2022

Comments

  • iPhone Developer
    iPhone Developer almost 2 years

    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
    - (void)requestFinished:(ASIHTTPRequest *)request 
    { 
        NSLog(@"REQUEST FINISHED"); 
        [aSpinner stopAnimating]; 
        //[aSpinner release]; 
    }