MBProgressHUD 到单例

MBProgressHUD to Singleton

我想在 Singleton 中设置显示和关闭 HUD 的方法。我在单例中写了这个方法:

- (void)showHUD
{
    activityHud = [[MBProgressHUD alloc] init];
    activityHud.delegate = self;
}

但什么也没发生。对于解雇,我不知道怎么写......任何答案都会很有帮助。谢谢!

为什么不使用静态方法?我使用它并将其放入我的 util class.

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

return hud;

}

并用这个隐藏它:

+ (void)dismissGlobalHUD {

[MBProgressHUD hideHUDForView:window animated:YES]; 
}

你可以做我做的。我有一个基础 VC class,我所有的 viewControllers subclass 都来自这个基础,并且有这些方法:

-(void)showActivityViewForView:(UIView *)view withActivityText:(NSString *)text;

-(void)hideActivityView;

这样实现的:

    -(void)showActivityViewForView:(UIView *)view withActivityText:(NSString *)text;
{
    self.viewForActivity = view;
    self.activitytext = text;
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(showActivityView) userInfo:nil repeats:NO];
}


-(void)showActivityView
{
    self.progressView = [MBProgressHUD showHUDAddedTo:self.viewForActivity animated:YES];
    self.progressView.opacity = 0.5;
    self.progressView.labelText = self.activitytext;
}

-(void)hideActivityView
{
    [self.timer invalidate];
    [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
}

计时器在那里,所以如果我完成的网络 activity 只需不到一秒的时间即可完成,我根本不会显示 MBProgressHUD

将它放在基础 class 中意味着我所有的 VC 都可以轻松访问这些方法。