简体   繁体   中英

Disable (login)button when logging in

Hello i'm working on this app which has a login section. What i want is when i click the login button that button has to be unclickable until the login succeeds or fails. I already tried adding this line doLogin.enabled = NO; But that wasn't useful. Please help. This is my code:

- (IBAction)doLogin:(id)sender {

    [self loginUser];
}

- (void)loginUser
{

    if (![self.usernameBox.text isEqualToString:@""] && ![self.passwordBox.text isEqualToString:@""])
    {
        //TODO: check id email pattern is correct
        [self showLoginProcess:true];
        [[AuthSingleton getInstance] attemptLoginWithUsername:self.usernameBox.text andPassword:self.passwordBox.text withSuccesBlock:^(AFHTTPRequestOperation *operation, id responseObject)
         {
             [self showLoginProcess:false];

             UIViewController *newFrontController = nil;
             PaMapViewController * vc = [[PaMapViewController alloc] init];
             newFrontController = [[UINavigationController alloc] initWithRootViewController:vc];

             SWRevealViewController *revealController = self.revealViewController;
             [revealController pushFrontViewController:newFrontController animated:YES];
         } andFailureBlock:^(AFHTTPRequestOperation *operation, NSError *error)
         {
             NSDictionary *dic = [error.userInfo objectForKey:@"JSONResponseSerializerWithDataKey"];
             #ifdef DEBUG
             NSLog(@"dic = %@", dic);
             #endif
             if ([[dic objectForKey:@"error_uri"] isEqual:@"phone"])
             {
                 NSError *jsonError;
                 NSData *objectData = [[dic objectForKey:@"error_description"] dataUsingEncoding:NSUTF8StringEncoding];
                 NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objectData
                                                                      options:NSJSONReadingMutableContainers
                                                                        error:&jsonError];
                 [self loginFailed:json];
             }
             else
             {
                 [self loginFailed:dic];
             }
         }];
    }
    else
    {
        //TODO: show proper message Test
        NSLog(@"username or password is empty %@", kBaseURL);
    }
}

- (void)showLoginProcess:(BOOL) show
{
    [self.spinner setColor:[UIColor whiteColor]];
    self.spinner.hidden = !show;
    self.usernameBox.hidden = show;
    self.passwordBox.hidden = show;
    if (show)

    {
        [self.spinner startAnimating];
    } else
    {
        [self.spinner stopAnimating];
    }
}

Instead of

doLogin.enabled = NO

write

doLogin.userInteractionEnabled = NO

Hope you have declared a property for the login button. Let it be "doLogin".

What you need to do is

- (IBAction)doLogin:(id)sender

{

[self loginUser];
doLogin.userInteractionEnabled = NO;

}

and when login succeeds or fails write

doLogin.userInteractionEnabled = YES;

within the corresponding block.

 - (IBAction)doLogin:(id)sender 
{
    doLogin.userInteractionEnabled = NO;
    [self loginUser];
    doLogin.userInteractionEnabled = YES;
}

Given below is the code snipe that should help

- (IBAction)loginUser:(id)sender 
{
//before login disable the user interaction of the button
loginButton.userInteractionEnabled = NO;

/*Your login logic here*/

// after successful login enable the login button once again
loginButton.userInteractionEnabled = YES;
}

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