当我的应用程序正在检查用户身份验证时,我想显示一个活动指示器来代替我的右侧 UIBarButtonItem。
当前发生的情况是用户单击登录按钮,然后 UI 暂停并等待 Web 调用返回。有人建议我使用调度队列在不同的线程上运行它,但我不确定如何组合这两段代码:
调度队列:
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^ {
//show your indicator button
dispatch_async(dispatch_get_main_queue(), ^ {
//stop indicator when user has logged in
});
});
登录方式:
- (void) loginAction{
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc]
initWithFrame:CGRectMake(0, 0, 20, 20)];
[activityIndicator startAnimating];
UIBarButtonItem *activityItem = [[UIBarButtonItem alloc]
initWithCustomView:activityIndicator];
self.navigationItem.rightBarButtonItem = activityItem;
if ([userNameTextField.text isEqualToString:@""] || [passwordTextField.text
isEqualToString:@""]) {
UIBarButtonItem *btnGo = [[UIBarButtonItem alloc] initWithTitle:@"Login"
style:UIBarButtonItemStyleBordered target:self action:@selector(loginAction)];
self.navigationItem.rightBarButtonItem = btnGo;
[self.navigationItem.rightBarButtonItem setTintColor:[UIColor colorWithRed:44.0/255.0
green:160.0/255.0 blue:65.0/255.0 alpha:1.0]];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Invalid Login" message:@""
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
return;
}
if (loginSuccess== true) {
//continue
}
}else
{
UIBarButtonItem *btnGo = [[UIBarButtonItem alloc] initWithTitle:@"Login"
style:UIBarButtonItemStyleBordered target:self action:@selector(loginAction)];
self.navigationItem.rightBarButtonItem = btnGo;
[self.navigationItem.rightBarButtonItem setTintColor:[UIColor colorWithRed:44.0/255.0
green:160.0/255.0 blue:65.0/255.0 alpha:1.0]];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Invalid Login" message:@""
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
return;
}
}