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.
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];
}
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];
}
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];
Remember also to dismiss alert in main thread:
dispatch_async(dispatch_get_main_queue(), ^{
[self.alert dismissWithClickedButtonIndex:0 animated:YES];
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With