0

我有以下代码 - 它创建一个 UIActivityIndi​​catorView 并在单击按钮时执行登录功能。我需要在调用函数之前显示一段时间的活动指示器,所以我做了 sleep(10),但问题是按钮卡在按下状态(背景颜色从白色变为蓝色并冻结)并且活动指示器视图不显示。当我删除 sleep(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"];
    }
}
4

1 回答 1

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"];
    }
}
于 2012-12-27T08:33:43.943 回答