这是因为您立即将 HUD 隐藏在 insertDetails 中。
-(IBAction)addtocontacts:(id)sender
{
HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
HUD.labelText = NSLocalizedString(@"Saving_data", @"");
//added my validation here
[self performSelectorInBackground:@selector(insertDetails) withObject:nil];
}
-(void) insertDetails
{
//save contact details in database
// [HUD hide:YES]; remove it
UIAlertView *alertview=[[UIAlertView alloc]initWithTitle:@"" message:@"Contact account details added" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alertview show];
}
而是下载较新版本的 MBProgressHUD 并使用以下方法
- (IBAction)addtocontacts:(id)sender {
// The hud will dispable all input on the view (use the higest view possible in the view hierarchy)
HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
[self.view addSubview:HUD];
// Regiser for HUD callbacks so we can remove it from the window at the right time
HUD.delegate = self;
// Show the HUD while the provided method executes in a new thread
[HUD showWhileExecuting:@selector(myTask) onTarget:self withObject:nil animated:YES];
}
并实现委托方法。
MBProgressHUDDelegate 方法
- (void)hudWasHidden:(MBProgressHUD *)hud {
// Remove HUD from screen when the HUD was hidded
[HUD removeFromSuperview];
[HUD release];
HUD = nil;
}