登录时禁用(登录)按钮

时间:2015-06-10 09:08:22

标签: ios objective-c

您好我正在使用此应用程序,该应用程序具有登录部分。我想要的是当我点击登录按钮时该按钮必须是不可点击的,直到登录成功或失败。我已经尝试添加此行doLogin.enabled = NO; 但这并不有用。请帮忙。这是我的代码:

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

4 个答案:

答案 0 :(得分:1)

而不是

doLogin.enabled = NO

doLogin.userInteractionEnabled = NO

答案 1 :(得分:1)

希望您已为登录按钮声明了一个属性。让它成为“doLogin”。

您需要做的是

- (IBAction)doLogin:(id)sender

{

[self loginUser];
doLogin.userInteractionEnabled = NO;

}

当登录成功或失败时写

doLogin.userInteractionEnabled = YES;

在相应的区块内。

答案 2 :(得分:0)

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

答案 3 :(得分:0)

以下是应该帮助的代码狙击

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