Use of MBProgressHUD Globally + make it singleton

后端 未结 9 1236
时光说笑
时光说笑 2020-12-23 02:06

In my Project, each of the user interaction events make a network call (Which is TCP, not HTTP). I need Activity Indicator to be global to show from a rando

9条回答
  •  余生分开走
    2020-12-23 02:37

    NOTE...

    as with many iOS issues, this is now drastically, totally out of date.

    These days you certainly just use a trivial

    Container view

    for any issue like this.

    Full container view tutorial for beginners .. tutorial!

    MBProgressHUD was a miraculous solution back in the day, because there was a "drastic hole" in Apple's pipeline.

    But (as with many wonderful things from the past), this is only history now. Don't do anything like this today.


    Just FWIW, 2014, here's a very simple setup we use. Per David Lawson...

    UIWindow *window = [[UIApplication sharedApplication] delegate].window
    

    as Matej says, just use AppDelegate...

    #define APP ((AppDelegate *)[[UIApplication sharedApplication] delegate])
    

    AppDelegate.h

    // our convenient huddie system (messages with a hud, spinner)
    @property (nonatomic, strong) MBProgressHUD *hud;
    -(void)huddie;
    

    AppDelegate.m

    -(void)huddie
    {
    // centralised location for MBProgressHUD
    [self.hud hide:YES];
    
    UIWindow *windowForHud = [[UIApplication sharedApplication] delegate].window;
    self.hud = [MBProgressHUD showHUDAddedTo:windowForHud animated:YES];
    
    self.hud.dimBackground = YES;
    self.hud.minShowTime = 0.1;
    self.hud.labelText = @"";
    self.hud.detailsLabelText = @"";
    }
    

    Set the titles in your code where you are using it - because you very often change them during a run. ("Step 1" ... "Step 2" etc)

    -(void)loadBlahFromCloud
    {
    [APP huddie];
    APP.hud.labelText = @"Connecting to Parse...";
    APP.hud.detailsLabelText = @"step 1/2";
    
    [blah refreshFromCloudThen:
        ^{
        [... example];
        }];
    }
    
    -(void)example
    {
    APP.hud.labelText = @"Connecting to the bank...";
    APP.hud.detailsLabelText = @"step 2/2";
    
    [blah sendDetailsThen:
        ^{
        [APP.hud hide:YES];
        [...  showNewDisplay];
        }];
    }
    

    Change huddle to take the texts as an argument if you wish

    You always want self.hud.minShowTime = 0.1; to avoid flicker

    Almost always self.hud.dimBackground = YES; which also blocks UI

    Conceptually of course you usually have to "slightly wait" to begin work / end work when you bring up such a process, as with any similar programming with the UI.

    So in practice code will usually look like this...

    -(void)loadActionSheets
    {
    [APP huddie];
    APP.hud.labelText = @"Loading json from net...";
    
    dispatch_after_secs_on_main(0.1 ,
        ^{
        [STUBS refreshNowFromCloudThen:
            ^{
            [APP.hud hide:YES];
            dispatch_after_secs_on_main(0.1 , ^{ [self buildActionsheet]; });
            }];
            }
        );
    }
    

    Handy macro ..

    #define dispatch_after_secs_on_main( SS, BB )                   \
            dispatch_after(                                         \
                dispatch_time(DISPATCH_TIME_NOW, SS*NSEC_PER_SEC),  \
                dispatch_get_main_queue(),                          \
                BB                                                  \
                )
    

    This is all history now :) https://stackoverflow.com/a/23403979/294884

提交回复
热议问题