我想设置在Singleton中显示和解雇HUD的方法。我用singleton编写这个方法:
- (void)showHUD
{
activityHud = [[MBProgressHUD alloc] init];
activityHud.delegate = self;
}
但没有发生任何事。对于解雇,我不知道如何写...任何答案都会非常有帮助。谢谢!
答案 0 :(得分:1)
为什么不使用静态方法?我使用它并将它放在我的util类中。
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];
}
答案 1 :(得分:1)
你可以做我做的事。我有一个基础VC类,我的所有viewControllers都是子类,并且有这些方法:
-(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];
}
计时器就在那里,如果我所做的网络活动需要不到一秒的时间才能完成,我根本不会显示MBProgressHUD
。
将它放在基类中意味着我所有的VC都可以轻松访问这些方法。