在子线程中显示提示信息框的公共方法

本文介绍了一种在iOS应用中从子线程调用主线程以显示UIAlertView的方法。通过将必要的参数打包成NSArray,并使用performSelectorOnMainThread方法确保UIAlertView在主线程中被正确展示。

 

#pragma mark - 在子线程中显示提示信息框

//在子线程中显示提示信息框
-(void)showAlertwithTitle:(NSString *)title message:(NSString *)msg cancleButtonTitle:(NSString *) cancleTitle otherButtonTitle:(NSString *)otherButtonTitle tag:(NSString *)tag
{
    NSArray *array = [NSArray arrayWithObjects:title,msg,cancleTitle,otherButtonTitle,tag, nil];
    [self performSelectorOnMainThread:@selector(doAlert:) withObject:array waitUntilDone:NO];   //回归主线程
}

//弹出信息显示框
-(void)doAlert:(NSArray *)array
{
    NSString *title = nil;
    if (![[array objectAtIndex:0] length]==0) title = [array objectAtIndex:0];

    NSString *msg = nil;
    if (![[array objectAtIndex:1] length]==0) msg = [array objectAtIndex:1];

    
    NSString *cancleTitle = nil;
    if (![[array objectAtIndex:2] length]==0) cancleTitle = [array objectAtIndex:2];
    
    NSString *otherButtonTitle = nil;
    if (![[array objectAtIndex:3] length]==0) otherButtonTitle = [array objectAtIndex:3];

    
    NSString *tag = @"0";
    if (![[array objectAtIndex:4] length]==0) tag = [array objectAtIndex:4];

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:cancleTitle otherButtonTitles:otherButtonTitle,nil];
    alert.tag = [tag integerValue];
    [alert show];
    [alert release];
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值