UIActivityIndicatorView does not display directly on UIAlertView

I am writing an iOS application and I need to display a UIAlertView with a counter. Sometimes something goes wrong when I try to add a counter to the warning center, usually when there was another warning before (not quite correct, but this is how I noticed that it does not work).

I partially resolved this error by delaying the creation of the counter. This is how I do it (warning is a member of 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 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];
}

, , . , , . iOS , ? - " , addSpinner"? , , ...

, - ! .

+5
4

https://github.com/jdg/MBProgressHUD.

, .

-(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];
}
+5

UIActivityIndicator UIAlertView. UIAlertView, , .

//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];
}
+4

try it

[[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 turn off the indicator, use this code

[megaAlert dismissWithClickedButtonIndex:0 animated:YES];
megaAlert = nil;
[indicator stopAnimating];
+2
source

Remember also to turn off the alert in the main thread:

dispatch_async(dispatch_get_main_queue(), ^{
    [self.alert dismissWithClickedButtonIndex:0 animated:YES];
});
+1
source

All Articles