2015-06-10 188 views
2

你好我正在這個應用程序有一個登錄部分。我想要的是當我點擊登錄按鈕時,該按鈕必須是無法點擊的,直到登錄成功或失敗。我已經嘗試添加這條線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]; 
    } 
} 

回答

1

希望你已經宣佈的登錄按鈕的屬性。讓它成爲「doLogin」。

你需要做的是

- (IBAction)doLogin:(id)sender 

{ 

[self loginUser]; 
doLogin.userInteractionEnabled = NO; 

} 

,當登錄成功或失敗對應的塊內寫

doLogin.userInteractionEnabled = YES; 

1

寫而不是

doLogin.enabled = NO 

doLogin.userInteractionEnabled = NO 
+0

爲什麼,,爲什麼不這樣做'''enabled'''? –

+1

啓用/禁用是突出顯示按鈕的屬性。而用戶交互啓用設置則允許用戶與UI進行交互。例如,如果你寫btn.enable = false你的按鈕將看起來禁用,但你仍然可以點擊它。另一方面,如果你寫btn.userInteractionEnable = false,你不能點擊按鈕。 – iAnurag

0
- (IBAction)doLogin:(id)sender 
{ 
    doLogin.userInteractionEnabled = NO; 
    [self loginUser]; 
    doLogin.userInteractionEnabled = YES; 
} 
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; 
}