ios - MBprogressHUD 未按预期显示

标签 ios objective-c ios5 mbprogresshud

我正在使用 MBProgressHUD显示 HUD,但未按预期显示。

步骤: 用户在 tableView 中选择一个单元格。解析一些数据然后向用户显示警告 (UIAlertView)。系统会询问用户是否要与所选(单元格)用户一起创建新游戏。取消/开始游戏按钮。

UIAlertView委托(delegate)方法如下:

pragma mark - UIAlertView Delegate

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    NSLog(@"button %i",buttonIndex);
    if (buttonIndex == 0) {
        // Cancel button pressed
        self.gameNewOpponentuser = nil;
    } else {
        // Start Game button pressed
        [MESGameModel createNewGameWithUser:[PFUser currentUser] against:self.gameNewOpponentuser];
    }
}

如果用户选择开始游戏,GameModel 方法就会运行。游戏模型(NSObject子类)方法如下:

pragma mark - 自定义方法

+(void)createNewGameWithUser:(PFUser *)user1 against:(PFUser *)user2 {
    // First we put a HUD up for the user on the window
    MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithWindow:[UIApplication sharedApplication].keyWindow];
    HUD.dimBackground = YES;
    HUD.labelText = NSLocalizedString(@"HUDCreateNewGame", @"HUD - Create New Game text");
    HUD.removeFromSuperViewOnHide = YES;
    
    // Confirm we have two users to play with.
    
}

如您所见,HUD 已将初始化分配给应用程序的关键窗口。然而,HUD 没有按预期显示,没有任何反应,没有 UI 锁定等。我在上面的方法中放置了一个断点,可以看到它被调用但是 HUD 没有显示

从上面的内容来看,我希望 HUD 会出现,但没有其他任何事情发生,即 HUD 目前只是留在屏幕上...

最佳答案

您缺少将 HUD 添加到 Window 然后显示 HUD 的部分。请注意,您可以将 HUD 添加到 Window 或当前 View (self.view)。

MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithWindow:[UIApplication sharedApplication].keyWindow];
[[UIApplication sharedApplication].keyWindow addSubview:HUD]; //<-- You're missing this

HUD.dimBackground = YES;
HUD.labelText = NSLocalizedString(@"HUDCreateNewGame", @"HUD - Create New Game text");
HUD.removeFromSuperViewOnHide = YES;

[HUD showAnimated:YES whileExecutingBlock:^{ //<-- And this
  // Do something
}];

关于ios - MBprogressHUD 未按预期显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16958441/

相关文章:

iphone - CoreData 和 NSInvalidArgumentException 无法识别的选择器发送到实例

ios - 为 RideIntent 创建自定义 CLPlacemark

ios - 等待多个异步任务完成

生成视频缩略图的 iOS 代码返回 0 字节[]

ios - UISegmentedControl 在删除段时删除当前选定的段

ios - 允许用户在 UIImage 上绘制矩形,以裁剪图像

php - 使用 PHP 向 Urban 飞艇发送消息

ios - 如何验证我的应用程序是否支持 iOS 4.3

ios - 上传图片到Cloudinary IOS

objective-c - 如何使用 Objective C/iOS 5 SDK 安装 Protocol Buffer ?