我希望在我的应用程序检查用户身份验证时显示一个活动指示符来代替我的右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;
}
}
答案 0 :(得分:2)
我遇到过同样的问题。我已经修好了这个。在viewdidload中,我已将活动指示器添加为子视图,并在单击登录按钮时开始设置指示器的动画。然后使用performSelector我调用了一个包含登录服务调用的方法,最后我只是停止指示器。它对我来说很好。使用此代码..
.H UIActivityIndicatorView * activityIndicator;
在.m
- (void)viewDidLoad
{
[super viewDidLoad];
activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[activityIndicator setFrame:CGRectMake(0, 0, 37, 37)];
[activityIndicator setCenter:self.view.center];
[activityIndicator setHidesWhenStopped:YES];
[self.view addSubview:activityIndicator];
[self setDisplayStatusOfControls];
}
-(IBAction)loginButton
{
[activityIndicator startAnimating];
[self performSelector:@selector(Loginauthentication) withObject:nil afterDelay:0.1f];
}
- (void) loginAction
{
//Log in authentication..
[activityIndicator stopAnimating];
}
答案 1 :(得分:1)
做这样的事情:
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
});
});
答案 2 :(得分:0)
UI只能在主线程上更新。当主线程正在做其他事情时,ui无法更新。要更新您的活动指标,您可以执行以下操作:
[self performSelectorOnMainThread:@selector(loginAction:) withObject:anObject waitUntilDone:NO];