Ales
Ales

Reputation: 19

Show AlertView when tapping tab in TabBar

What I need

I'd like to show an UIAlertView "Loading..." when I tap on a tab in my tabBar.

Reason why I want to do that

This is because that particular tab contains a UIWebView and is very slow, so when I tap on that tab it seems like my app is doing absolutely nothing for a few seconds. It is very ugly, so I'd like to show something to make the user know that the app is actually working.

Questions

In which position do I have to put it? If I write it in the ViewDidLoad, the AlertView is shown after those orrible few seconds. Is AlertView the best choice?

Thank you all.

Upvotes: 0

Views: 297

Answers (3)

E-Riddie
E-Riddie

Reputation: 14780

Set your tabBar as a delegate on viewDidLoad

- (void)viewDidLoad
{
    [super viewDidLoad];

    //Setting tabBarController as a delegate
    self.tabBarController.delegate = self;
}

On your interface at .h method add delegate <UITabBarControllerDelegate>

Implement this function and add your loading view, for each time that you press tabBar. The method below gets executed always when the user taps an item on tabbar.

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
    {
        //Show loading form here
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
            // Do something...
            dispatch_async(dispatch_get_main_queue(), ^{
                [MBProgressHUD hideHUDForView:self.view animated:YES];
            });
        });
    }

P.S You need to add this method and set to delegate to every viewController which you need to show the alert view (Loading View)

For a loading view you can also load use MBProgressHUD as KingBabar pointed out. It has great features.

Upvotes: 0

Maverick
Maverick

Reputation: 319

UIAlerView is not a good idea.

//Delegate method of UIWebView
- (void)webViewDidStartLoad:(UIWebView *)webView 
{
    if(![[UIApplication sharedApplication] isNetworkActivityIndicatorVisible])
    {
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
    }
}

//Delegate method of UIWebView
- (void)webViewDidFinishLoad:(UIWebView *)webView_ 
{
    if([[UIApplication sharedApplication] isNetworkActivityIndicatorVisible])
    {
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
    }
}

Upvotes: 1

Tomer Even
Tomer Even

Reputation: 4980

add MBProgressHUD to your UIWebView viewDidLoad method

- (void)viewDidLoad {
    [super viewDidLoad];
    self.webView.delegate = self;
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"Loading...";
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
      [hud hide:YES];
}

Upvotes: 1

Related Questions