简体   繁体   中英

Display Activity Indicator in Place of Login Button Immediately Upon User Click

I would like to display an activity indicator in place of my right UIBarButtonItem while my app is checking the users authentication.

What is currently happening, is the user clicks the login button, then the UI pauses and waits for the web call to return. It has been recommended that I use a dispatch queue to run this on a different thread, but Im not sure how to combine these two pieces of code:

Dispatch Queue:

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
});
});

Login Method:

- (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;

}
}

I have faced this same issue. i have fixed like this. In viewdidload i have added the activity indicator as a sub view and start animating the indicator while clicking the login button. Then with performSelector i have called a method that containing the login service call, at the end i just stop the indicator. Its working fine for me. Use this code..

.h UIActivityIndicatorView *activityIndicator;

In .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];
}

Make something like this:

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
    });
});

UI can be update only on the main thread. When main thread is doing something else then ui can not be updated. To update your activity indicator you can do as following:

[self performSelectorOnMainThread:@selector(loginAction:) withObject:anObject waitUntilDone:NO];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM