布尔人没有设置在正确的时机



我有一个我将在登录流中使用的BOOL loginStatus


代码

- (IBAction)login:(id)sender {
    if ([self credentialsValidated]) {
            [self performSegueWithIdentifier:@"showLogin" sender:self];
     }else {
       NSLog(@"not valid");
     }
}
- (BOOL)credentialsValidated {
     [[API sharedInstance] loginCommand:[NSMutableDictionary dictionaryWithObjectsAndKeys:_txtLogin.text,@"email",_txtPass.text,@"pwd", nil] onCompletion:^(NSDictionary *json){
     //completion
         if(![json objectForKey:@"error"]){
             if([[json valueForKeyPath:@"data.status"]intValue] == 200){
                //Create user object
                 NSLog(@"tot hier");
                loginstatus =  YES;
             }else{
                 //show validation
                 NSString *message = [NSString stringWithFormat:@"%@",[json valueForKeyPath:@"data.text"]];
                 [[[UIAlertView alloc]initWithTitle:@"Fout" message:message delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]show];
                 _txtLogin.text = @"";
                 _txtPass.text = @"";
                 loginstatus = NO;
             }
         }else {
             NSLog(@"Cannot connect to the server");
         }
     }];
     if(loginstatus){
         NSLog(@"true");
    }else{
        NSLog(@"false");
    }
     return loginstatus;
}

我遇到的问题是我的BOOL没有立即设置。在正确设置布尔值之前,我总是需要两次按登录按钮。

有什么想法?

没有立即设置布尔值,因为它是在完成块中设置的,直到异步操作完成。您应该做点什么让用户在API工作时等待。

例如,您可以禁用字段并显示旋转进度指示器,直到登录操作完成为止。您将在调用登录之前配置为禁用的UI并将其设置为在完成块中启用。

最新更新