-(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];
UIAlertView *alertview=[[UIAlertView alloc]initWithTitle:@"" message:@"Contact account details added" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alertview show];
}
我在savebutton点击添加了加载符号,但是没成功,怎么让符号出现,到了有警告信息时再消失?
performSelectorInBackground这里马上就去运行了,同时[HUD hide:YES];又让loading动画隐藏了。
你可以试着注释掉[HUD hide:YES];
另外,有关UI的操作,最好都不要放到Background线程里,因此performSelectorInBackground最好改为performSelector
添加HUD到顶层布局中。
比如你在视图中的顶层有tableView,就添加到tableView中。我就添加了HUD到tableView中:
[MBProgressHUD showHUDAddedTo:self.my_tableView animated:YES];
在执行performSelectorInBackground后,会在后台新启一个线程来执行你的insertDetails方法。而HUD如果要显示出来则需要同步主线程,所以你不能使用performSelectorInBackground 这个方法。 你可以直接给对象发送insertDetails 消息,或使用performSelector来调用。
针对你上面的问题还有一点就是,如果是开启了一个后台线程,在后台线程中如果想要操作UI的话,应该注意:我们知道操作UI应该在主线程上,这样就出现了一个线程间交互的问题。如果在后台线程中想要操作UI的话,你需要使用performSelectorOnMainThread 在这个主线程上来执行更新UI的操作。