0

I've created an app where I'm creating a UIProgressBarHUD to show that something is loading. My question is, how can I disable the view so nothing can be pressed untill the loading is finished?

I've tried setting:

[self.view setUserInterationEnabled:NO];

However this doesn't work :/

Here is the code I'm using for adding the UIProgressHUD:

- (IBAction) showHUD:(id)sender
{
       //[self.view setUserInteractionEnabled:NO];

       UIProgressHUD *HUD = [[UIProgressHUD alloc]
initWithWindow:[[UIApplication sharedApplication] keyWindow]];
       [HUD setText:@"Loading…"];
       [HUD show:YES];

       [HUD performSelector:@selector(done) withObject:nil afterDelay:1.5];
       [HUD performSelector:@selector(setText:) withObject:@"Done!"
afterDelay:1.5];
       [HUD performSelector:@selector(hide) withObject:nil afterDelay:4.0];

       //[self.view setUserInteractionEnabled:YES];

       [HUD release];
}

Any help would be muchly appreciated!! - James

ingh.am
  • 25,981
  • 43
  • 130
  • 177

4 Answers4

3

In MBProgressHUD.m

#define APPDELEGATE                         
(TMAppDelegate *)[[UIApplication sharedApplication]delegate]

- (void)show:(BOOL)animated 
{
    [[APPDELEGATE window] setUserInteractionEnabled:NO]; //use this line
}

- (void)hide:(BOOL)animated 
{  
    [[APPDELEGATE window] setUserInteractionEnabled:YES]; //use this line
}
akjoshi
  • 15,374
  • 13
  • 103
  • 121
Paras
  • 31
  • 1
3

You can disable user interaction with the nifty property named userInteractionsEnabled, that is defined for UIView. It just so happens that UIWindow is a subclass of UIView, we we can easily disable user interactions for out whole app.

anyViewInYouApp.window.userInteractionsEnabled = NO;

Or keep a reference to the window if you like.

PeyloW
  • 36,742
  • 12
  • 80
  • 99
1

As pointed out here, UIProgressHUD is private. You should not use it.

There is a library that gives you what you are looking for though.

It allows you to keep the user from tapping anything while it is updating as you requested.

Community
  • 1
  • 1
Matt Long
  • 24,438
  • 4
  • 73
  • 99
  • Oh right, I've always read to be careful with non documented features. Why on earth would apple have this available if you aren't allowed to use it? I'll give the MBProgressHUD a go.. Thanks – ingh.am Oct 02 '09 at 08:52
  • I've found that this method is good but when continuously pressing the button to show the HUD it will crash within no time :/ – ingh.am Oct 02 '09 at 13:16
  • Have you used this code? By removing the HUD release in hudWasHidden then it no longer crashes.. hmm – ingh.am Oct 02 '09 at 13:25
0
UIWindow *win = [UIApplication sharedApplication].keyWindow;
[win setUserInteractionEnabled:NO];
SachinVsSachin
  • 6,401
  • 3
  • 33
  • 39