Use of MBProgressHUD Globally + make it singleton

后端 未结 9 1206
时光说笑
时光说笑 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:20

    You could add this to a class of your liking:

    + (MBProgressHUD *)showGlobalProgressHUDWithTitle:(NSString *)title {
        UIWindow *window = [[[UIApplication sharedApplication] windows] lastObject];
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:window animated:YES];
        hud.labelText = title;
        return hud;
    }
    
    + (void)dismissGlobalHUD {
        UIWindow *window = [[[UIApplication sharedApplication] windows] lastObject];
        [MBProgressHUD hideHUDForView:window animated:YES];
    }
    

    This can be than called on any class. You don't need to keep a strong reference to the HUD when using those class convenience methods.

    Depending on your specific situation you'll probably also want to handle cases where a new hud is requested before the other one is hidden. You could eater hide the previous hud when a new comes in or come up with some sort of queueing, etc.

    Hiding the previous HUD instance before showing a new one is pretty straightforward.

    + (MBProgressHUD *)showGlobalProgressHUDWithTitle:(NSString *)title {
        UIWindow *window = [[[UIApplication sharedApplication] windows] lastObject];
        [MBProgressHUD hideAllHUDsForView:window animated:YES];
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:window animated:YES];
        hud.labelText = title;
        return hud;
    }
    

提交回复
热议问题