MBProgressHUD无法显示

时间:2016-03-15 06:22:05

标签: ios

我正在使用MBProgressHUD向用户显示提示,但它没有显示。有许多viewcontrollers使用相同的方法。只有一个页面无法显示,此视图控制器是使用xib创建的。 方法如下:

-(void)creatHUD:(NSString *)hud {

    if (!HUD) {
        HUD = [[MBProgressHUD alloc] initWithView:self.view] ;
        [self.view addSubview:HUD];
        HUD.delegate = self;
    }
    HUD.labelText = hud;
}

4 个答案:

答案 0 :(得分:2)

试试此代码

UIView *window = [UIApplication sharedApplication].keyWindow;

MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:window animated:YES];

[hud show:YES];

希望这会有用......

答案 1 :(得分:0)

由于问题不明确,我认为你试图用后台线程来展示。请试试这个。

dispatch_async(dispatch_get_main_queue(), ^{
        //show Hud code here
    });

答案 2 :(得分:0)

我也使用MBProgressHUD,我的实现是:
1.仅在[UIApplication sharedApplication].keyWindow中显示MBProgressHUD 2.切勿在视图中添加多个Hud 我打包它,这是我的代码:

#define APP [UIApplication sharedApplication]
+ (MBProgressHUD *)Hud
{
    MBProgressHUD *hud = [MBProgressHUD HUDForView:APP.keyWindow];
    if (hud){
        [hud removeFromSuperview];
    }else{
        hud = [[MBProgressHUD alloc] initWithWindow:APP.keyWindow];
        hud.removeFromSuperViewOnHide = YES;
    }
    [APP.keyWindow addSubview:hud];
    return hud;
}

+ (void)showMsg:(NSString *)message
{
    MBProgressHUD *hud = [self Hud];
    hud.labelText = message;
    hud.mode = MBProgressHUDModeText;
    hud.labelFont = [UIFont systemFontOfSize:13];
    [hud show:YES];
    [hud hide:YES afterDelay:1.0];
}

+ (void)showLoading
{
    [self showLoading:@"please wait..."];
}

+ (void)showLoading:(NSString *)message
{
    MBProgressHUD *hud = [self Hud];
    hud.labelText = message;
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.labelFont = [UIFont systemFontOfSize:13];
    [hud show:YES];
}

+ (void)hideHUD
{
    MBProgressHUD *hud = [MBProgressHUD HUDForView:APP.keyWindow];
    [hud hide:YES];
}

答案 3 :(得分:0)

如果您正在快速开发中尝试这个

DispatchQueue.main.async {

        MBProgressHUD.showAdded(to: UIApplication.shared.keyWindow!, animated: true)
    }