Show HUD only DURING commands or actions?

Discussion in 'iOS Development' started by gojohnnyboi, May 25, 2008.

  1. gojohnnyboi

    gojohnnyboi Well-Known Member

    Joined:
    Jan 25, 2008
    Messages:
    3,339
    Likes Received:
    55
    Ok, i've been messing around with UIProgressHUD's a little today, and was wondering if there was a way to, while a hud is showing, perform some commands, and then hide the hud once they are done..

    now, i tried

    Code:
    [self performSelectorOnMainThread:@selector(hideHUD) withObject:nil waitUntilDone: YES
    but no luck..

    help?

    Thanks,
    -John
  2. atomjack

    atomjack New Member

    Joined:
    May 14, 2008
    Messages:
    3
    Likes Received:
    0
    I'm very new to Objective-C and only just learned all this in the past few days (combing through source code to various apps), but I was able to figure this out. I'm not sure why you have to actually separate stuff out into two methods (doSomething and doSomethingElse), but if you don't, the HUD doesn't seem to show (or else it shows and disappears right away).


    Interface:
    Code:
    @interface foo : Object {
    UIProgressHUD *_refreshProgressHUD;
    }
    - (void)openProgressHUD:(NSString*)message;
    - (void)closeProgressHUD;
    -(void)doSomething
    -(void)doSomethingElse
    @end
    
    
    Code:
    @implementation foo
    - (void)applicationDidFinishLaunching:(GSEventRef)event;
        struct CGRect rect;
    	rect = [UIHardware fullScreenApplicationContentRect];
        rect.origin.x = rect.origin.y = 0;
    
        mainWindow = [[UIWindow alloc] initWithContentRect:rect];
    
        [mainWindow setContentView:[[[UIView alloc] initWithFrame:[mainWindow bounds]] autorelease]];
    // add some stuff like a button that calls the doSomething method to the mainWindow
    
    }
    
    -(void)doSomething {
    [self openProgressHUD:@"doing something"];
    [self performSelector:@selector(doSomethingElse) withObject:nil afterDelay:0];
    }
    
    -(void)doSomethingElse {
    NSLog(@"doing something else here");
    sleep(2); // sleep for 2 seconds
    [self closeProgressHUD];
    }
    
    - (void)openProgressHUD:(NSString*)message
    {
    	NSLog(@"showing hud");
        _refreshProgressHUD = [[UIProgressHUD alloc] initWithWindow:mainWindow];
        [_refreshProgressHUD setText:message];
        [_refreshProgressHUD setFontSize:12];
        [_refreshProgressHUD show:YES];
    }
    
    - (void)closeProgressHUD
    {
        if ( _refreshProgressHUD ) {
            [_refreshProgressHUD show:NO];
            [_refreshProgressHUD release];
            _refreshProgressHUD = nil;
            [_refreshProgressHUD removeFromSuperview];
        }
    }
    
    
    
    
  3. gojohnnyboi

    gojohnnyboi Well-Known Member

    Joined:
    Jan 25, 2008
    Messages:
    3,339
    Likes Received:
    55
    So, you are saying this did or did not work for you?
  4. atomjack

    atomjack New Member

    Joined:
    May 14, 2008
    Messages:
    3
    Likes Received:
    0
    Yeah it works for me. Give the code a try. Once you trigger the doSomething method, the HUD should show up and it'll stay there for 2 seconds then disappear. Watch your system log (/var/log/system.log) to see the NSLog lines.

Share This Page