Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIActivityIndicatorView not displaying right on UIAlertView

I am writing an iOS app, and I have to display a UIAlertView with a spinner. Sometimes, something goes wrong when I try to add the spinner in the center of the alert, usually when there was another alert right before this one (not exactly a rule, but this is how I noticed it fails).

I partly solved this bug by delaying the creation of the spinner. Here is how I do it (alert is a member UIAlertView) :

This code is in viewDidLoad :

alert = [[[UIAlertView alloc] initWithTitle:@"Loading..." message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles: nil] autorelease];

[alert show];

[self performSelector:@selector(addSpinner) withObject:nil afterDelay:0.5f];

And this one is my addSpinner function :

- (void) addSpinner{

    UIActivityIndicatorView *indicator2 = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

    indicator2.center = CGPointMake(alert.bounds.size.width / 2, alert.bounds.size.height - 50);

    [indicator2 startAnimating];
    [alert addSubview:indicator2];
    [indicator2 release];
}

This solution is working every time, but I really don't like how it's done. Even though showing the spinner half a second after the alert pops up is not so much disturbing, there must be a better way to handle this. I am not an expert in iOS, nor am I a senior programmer, but can't I use an event ? Something like "when the alert is actually displayed, go the addSpinner function" ? I have no idea how to do this, and could not find much help on the web...

Hope someone can help me ! Thanks.

like image 600
rdurand Avatar asked Jul 10 '12 07:07

rdurand


4 Answers

You can use https://github.com/jdg/MBProgressHUD instead.

and for spinner within alert, you can use these methods.

-(void)startLoading
{
    alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Please Wait..." delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];

    UIActivityIndicatorView *progress= [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(125, 50, 30, 30)];
    progress.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
    [alert addSubview:progress];
    [progress startAnimating];
    [progress release];
    [alert show];
}
-(void)stopLoading
{
    [alert dismissWithClickedButtonIndex:0 animated:YES];
    [alert release];
}
like image 71
waheeda Avatar answered Nov 08 '22 01:11

waheeda


Basically problem looks to be Adding UIActivityIndicator before UIAlertView is displayed. Try using UIAlertView Delegate method to add the Indicator as below.

//AlertView delegate methods
- (void)didPresentAlertView:(UIAlertView *)alertView
{
    UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];

    indicator.center = CGPointMake(alertView.bounds.size.width/2, alertView.bounds.size.height/3 * 2);

    [indicator startAnimating];
    [alertView addSubview:indicator];
}
like image 23
suresh Avatar answered Nov 07 '22 23:11

suresh


Try this

[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
megaAlert = [[[UIAlertView alloc] initWithTitle:@"Please wait..."
                                                         message:nil delegate:self cancelButtonTitle:nil
                                               otherButtonTitles: nil] autorelease];
[megaAlert show];
indicator = [[UIActivityIndicatorView alloc]                                                      initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
            indicator.center = CGPointMake(megaAlert.bounds.size.width / 2,                                            megaAlert.bounds.size.height - 45);
[indicator startAnimating];
[megaAlert addSubview:indicator];
[indicator release];

To dismiss the Indicator use this code

[megaAlert dismissWithClickedButtonIndex:0 animated:YES];
megaAlert = nil;
[indicator stopAnimating];
like image 2
Ravi Kumar Karunanithi Avatar answered Nov 08 '22 00:11

Ravi Kumar Karunanithi


Remember also to dismiss alert in main thread:

dispatch_async(dispatch_get_main_queue(), ^{
    [self.alert dismissWithClickedButtonIndex:0 animated:YES];
});
like image 1
edzio27 Avatar answered Nov 08 '22 01:11

edzio27