2011-11-24 52 views
2

有人使用MBProgressHUD?我想在MBProgressHUD librairie提供的示例中使用動畫showWithLabelMixed。我將這個代碼從例子中複製到我的項目中。MBProgressHUD崩潰「在MBProgressHUD初始化器中使用的視圖是零」

-(IBAction)showActivity:(id)sender 
{ 
    NSLog(@"Show Activity"); 
    [self showWithLabelMixed]; 

} 

- (void)showWithLabelMixed{ 

    HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view]; 
    [self.navigationController.view addSubview:HUD]; 

    HUD.delegate = self; 
    HUD.labelText = @"Conecting"; 
    HUD.minSize = CGSizeMake(135.f, 135.f); 

    [HUD showWhileExecuting:@selector(myMixedTask) onTarget:self withObject:nil animated:YES]; 
} 


- (void)myTask { 
    // Do something usefull in here instead of sleeping ... 
    sleep(3); 
} 

- (void)myProgressTask { 
    // This just increases the progress indicator in a loop 
    float progress = 0.0f; 
    while (progress < 1.0f) { 
     progress += 0.01f; 
     HUD.progress = progress; 
     usleep(50000); 
    } 
} 




- (void)myMixedTask { 
    // Indeterminate mode 
    sleep(2); 
    // Switch to determinate mode 
    HUD.mode = MBProgressHUDModeDeterminate; 
    HUD.labelText = @"Progress"; 
    float progress = 0.0f; 
    while (progress < 1.0f) 
    { 
     progress += 0.01f; 
     HUD.progress = progress; 
     usleep(50000); 
    } 
    // Back to indeterminate mode 
    HUD.mode = MBProgressHUDModeIndeterminate; 
    HUD.labelText = @"Cleaning up"; 
    sleep(2); 
    // The sample image is based on the work by www.pixelpressicons.com, http://creativecommons.org/licenses/by/2.5/ca/ 
    // Make the customViews 37 by 37 pixels for best results (those are the bounds of the build-in progress indicators) 
    HUD.customView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]] autorelease]; 
    HUD.mode = MBProgressHUDModeCustomView; 
    HUD.labelText = @"Completed"; 
    sleep(2); 
} 

但我有一個崩潰

終止應用程序由於未捕獲的異常 「MBProgressHUDViewIsNillException」,理由是:「在 MBProgressHUD初始化所使用的觀點是零。」 你能幫我嗎?

回答

2

您正在使用初始化HUD視圖是零 嘗試用初始化它

[[UIApplication sharedApplication] keyWindow] 
+0

@DDanZimm哦,這是真的我沒有navbarcontroller,我用tabBarController。我改變初始化HUD = [[MBProgressHUD alloc] initWithView:self.tabBarController.view]; ,HUD = [[MBProgressHUD alloc] initWithView:self.view];在這兩種情況下,我沒有錯誤,但沒有出現。如何使用你給我的初始化? – Keviin55

+0

我必須做這個 HUD = [[MBProgressHUD alloc] initWithView:self.view]; [self.view addSubview:HUD]; – Keviin55

+0

很高興聽到您發現您的問題!感恩節快樂,如果你在美國! – DanZimm