我有以下代码 - 它创建一个UIActivityIndicatorView并在按钮点击时执行登录功能。我需要活动指示器在调用函数之前显示一段时间,所以我睡觉(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"];
}
}
答案 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"];
}
}