performSelectorOnMainThread 方法。程序开发中可以使用performselectorinbackground 来进行数据的获取,在performSelectorOnMainThread中进行UI的修改。
NSAutoreleasePool *pool =[[NSAutoreleasePool alloc] init];
[self performSelectorOnMainThread:@selector(RefreshCellForLiveId:)
withObject:userinfo
waitUntilDone:YES];
[pool release];
该方法的作用是在主线程中,执行制定的方法(代码块)。
参数:
@selector(RefreshCellForLiveId:)就是,要定义我们要执行的方法。
withObject:userinfo
定义了,我们执行RefreshCellForLiveId:方法时,传入的参数对象。类型是id。(我们可以传入任何参数)
waitUntilDone:YES];
指定,当前线程是否要被阻塞,直到主线程将我们制定的代码块(RefreshCellForLiveId:方法)执行完。
注意:
1.当前线程为主线程的时候,waitUntilDone:YES参数无效。
2.该方法,没有返回值
3.<span style="color:#FF0000;">该方法主要用来用主线程来修改页面UI的状态。例如跟新tableview
[self performSelectorOnMainThread:@selector(reloadtableview) withObject:nil waitUntilDone:YES];
</span>
例如使用HUD展示界面 后后面采用performselectorinbackground获取网络数据最后使用performSelectorOnMainThread 显示结果。
例如
if (HUD!=nil) {注意此时在返回主线程是最好将HUD关闭,执行[self hudWasHidden]函数
[HUD release];
}
HUD = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:HUD];
HUD.delegate = self;
HUD.labelText = @"正在发送信息...";
HUD.detailsLabelText = @"请稍候";
HUD.square = YES;
[HUD showWhileExecuting:@selector(SendBackProcess) onTarget:self withObject:nil animated:YES];
- (void)hudWasHidden {
// Remove HUD from screen when the HUD was hidded
[HUD removeFromSuperview];
[HUD release];
HUD=nil;
}
-(void)SendBackProcess ///////点赞
{
NSString *httpurl=[NSString stringWithFormat:@"%@/praisetopic",CMMUNITYURL];
int ret= [self SendActionData:httpurl];
if (ret!=0) {
[self hudWasHidden];
[self performSelectorOnMainThread:@selector(reloadtableview) withObject:nil waitUntilDone:YES];
// [DataTableView reloadData];
}
else
{
// [self LoadData];
[self hudWasHidden];
[self showAlert:@"网络错误。"];
}
// [self performSelectorOnMainThread:@selector(reloadtableview) withObject:nil waitUntilDone:YES];
}