点击按钮后暂停线程

时间:2012-12-27 08:25:32

标签: objective-c ios ios6

我有以下代码 - 它创建一个UIActivityIndi​​catorView并在按钮点击时执行登录功能。我需要活动指示器在调用函数之前显示一段时间,所以我睡觉(10),但问题是按钮卡在按下状态(背景颜色从白色变为蓝色并冻结)和activityindicatorview没有显示。当我删除睡眠(10)它工作正常。如何在创建指示器并且按钮返回正常状态后暂停线程?

-(IBAction)performLogin{
    UIActivityIndicatorView *view = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    [view setFrame:CGRectMake(180, 13, 10, 10)];
    [view startAnimating];
    [btnLogin addSubview:view];
    [sleep(10)];    
    if ([self validatePassword] == YES)
    {
        [self performSegueWithIdentifier:@"goToMain" sender:self];
    }
    else
    {
        [ErrorHandler showMessage:@"Wrong password" :@"Authentication"];
    }
}

1 个答案:

答案 0 :(得分:0)

将验证移至单独的方法,并在延迟后调用:

-(IBAction)performLogin{
    UIActivityIndicatorView *view = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    [view setFrame:CGRectMake(180, 13, 10, 10)];
    [view startAnimating];
    [btnLogin addSubview:view];
    [self performSelector:@selector(performPasswordValidation) withObject:nil afterDelay:10];
}

- (void)performPasswordValidation
{
    if ([self validatePassword] == YES)
    {
        [self performSegueWithIdentifier:@"goToMain" sender:self];
    }
    else
    {
        [ErrorHandler showMessage:@"Wrong password" :@"Authentication"];
    }
}