I am developing an app which has several viewControllers. The first one is “MainMenu” and the second one is “Page1”.
I would like to show an alert says “Please wait…” when it goes to the next page. My code below is working but the alert comes up after the “page1” is loaded. I would like it to come up when a user press a button on “MainMenu” page.
Do you have any suggestions to accomplish this?
Thanks in advance.
AppDelegate.m
-(void)showAlert{
altpleasewait = [[UIAlertView alloc] initWithTitle:@"Please Wait..." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
[altpleasewait show];
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
indicator.center = CGPointMake(altpleasewait.bounds.size.width / 2, altpleasewait.bounds.size.height - 50);
[indicator startAnimating];
[altpleasewait addSubview:indicator];
}
-(void)waitASecond{
[self performSelector:@selector(dismissAlert) withObject:self afterDelay:0.8];
}
-(void)dismissAlert{
[altpleasewait dismissWithClickedButtonIndex:0 animated:YES];
}
MainMenu.m
-(void)gotoNextPage{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
[appDelegate showAlert];
page1 = [self.storyboard instantiateViewControllerWithIdentifier:@"Page1"];
[self presentModalViewController:page1 animated:NO];
}
Page1.m
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
-------------some methods--------------
[appDelegate waitASecond];
This is just a suggestion. I would do the following.
In Page1.m in
-viewDidAppear, I would call the alert view to pop up. This is happening on the main thread. So I am not preventing UI from being displayed, or responsive.I would move all methods that are related to loading the
Page1content, e.g. texts that are to be read from a URL, into a block, and use GCD, so that they happen in the background, and not blocking the main thread.Once the loading is done…
Here is a simple tutorial that may give you an idea.
http://www.raywenderlich.com/4295/multithreading-and-grand-central-dispatch-on-ios-for-beginners-tutorial