0

当我的应用程序正在检查用户身份验证时,我想显示一个活动指示器来代替我的右侧 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;

}
}
4

3 回答 3

2

我也遇到过同样的问题。我已经这样修复了。在 viewdidload 中,我将活动指示器添加为子视图,并在单击登录按钮时开始为指示器设置动画。然后使用 performSelector 我调用了一个包含登录服务调用的方法,最后我只是停止了指标。它对我来说工作正常。使用此代码..

.h UIActivityIndi​​catorView *activityIndi​​cator;

在.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];
}
于 2013-03-07T10:01:53.347 回答
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
    });
});
于 2013-03-07T08:51:02.173 回答
0

UI 只能在主线程上更新。当主线程正在执行其他操作时,无法更新 ui。要更新您的活动指标,您可以执行以下操作:

[self performSelectorOnMainThread:@selector(loginAction:) withObject:anObject waitUntilDone:NO];
于 2013-03-07T09:07:00.050 回答