MBprogressHUD没有按预期显示

Stu*_*rtM 0 objective-c ios mbprogresshud ios5

MBProgressHUD用来显示HUD,但没有按预期显示.

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

UIAlertView委托方法如下:

pragma mark - UIAlertView代表

-(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];
    }
}
Run Code Online (Sandbox Code Playgroud)

如果用户选择"开始游戏",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.

}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,HUD是将alloc init分配给应用程序的keywindow.然而,HUD没有按预期显示,没有任何事情发生没有UI锁定等.我在上面的方法中放了一个断点,可以看到它被调用但是没有显示HUD

从上面看,我预计HUD会出现,但没有其他事情发生,即HUD现在只是留在屏幕上......

小智 5

您缺少将HUD添加到HUD Window然后显示HUD的部分.请注意,您可以将HUD添加到Window当前视图(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
}];
Run Code Online (Sandbox Code Playgroud)