0

In the method -(void)registerUser I present a modal view with 2 UITextFields and a Ok Button.

After filling the UITextFields and pressing the Ok Button I call the delegate method -(void)AEMUserRegistrationVCUserName:(NSString *)un password:(NSString *)pw where I verify the data connecting to a server.

When the answer arrives in -(void)request:(RKRequest *)request didLoadResponse:(RKResponse *)response If the data is wrong I try to set focus on the UITextfield with self.userRegistrationVC.userName becomeFirstResponder]but it does not get the focus.

I have checked [self.userRegistrationVC.userName canBecomeFirstResponder] and it returns NO, which on the other hand is what the docs say it is returned by default.

My code is here as a reference:

Note:

self.userNameand selfPasswordare NSStrings. self.userRegistrationVCis a UIViewController. self.userRegistrationVC.userNameand self.userRegistrationVC.passwordare UITextfields.

-(void)registerUser
{
    //Recuperar el nombre de usuario
    self.userName = [[NSUserDefaults standardUserDefaults] objectForKey:kNombreUsuario];
    if (!self.userName) {
        //No hay nombre de usuario, el usuario nunca ha registrado la aplicación.
        if (!self.userRegistrationVC) {
            //Solicitar datos de registro
            self.userRegistrationVC = [[AEMUserRegistrationViewController alloc] initWithNibName:@"AEMUserRegistrationViewController" bundle:nil];
        }
        self.userRegistrationVC.delegate = self;
        [self.viewControllerToPresentModalView presentModalViewController:self.userRegistrationVC animated:YES];
        return;
    }


    //Recuperar la contraseña
    NSError *error;
    self.password = [SFHFKeychainUtils getPasswordForUsername:self.userName andServiceName:kServiceName error:&error];
    if (!self.password) {
        //No hay contraseña. La contraseña se ha perdido.
        if (!self.userRegistrationVC) {
            //Solicitar datos de registro
            self.userRegistrationVC = [[AEMUserRegistrationViewController alloc] initWithNibName:@"AEMUserRegistrationViewController" bundle:nil];
        }
        self.userRegistrationVC.delegate = self;
        [self.viewControllerToPresentModalView presentModalViewController:self.userRegistrationVC animated:YES];
        return;
    }

    //Los datos del usuario existen
    //Verificar el registro
    [self.client get:kConfirmUsuario
         queryParams:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:self.userName, self.password, nil] 
                                                 forKeys:[NSArray arrayWithObjects:kNombreUsuario, kPassword, nil]]
                                                delegate:self];
}

-(void)AEMUserRegistrationVCUserName:(NSString *)un password:(NSString *)pw
{
    //El usuario ha introducido datos de registro
    //Realizar el registro
    self.userName = un;
    self.password = pw;
    [self.client get:kCreateUsuario 
         queryParams:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:self.userName, self.password, nil]
                                                 forKeys:[NSArray arrayWithObjects:kNombreUsuario, kPassword, nil]]
            delegate:self];

    //No hacer dismiss ahora esperar a verificar el registro
}


-(void)request:(RKRequest *)request didLoadResponse:(RKResponse *)response
{
    //Puede responder a createUsuario o a confirmUsuario
    //En ambos casos el error impide registrar al usuario y ejecutar el programa
    BOOL isRequestCreateUser;
    NSRange aRange = [request.resourcePath rangeOfString:kCreateUsuario];
    if (aRange.location != NSNotFound) { 
        //The request was to create a user
        isRequestCreateUser = YES;
    } else {
        //The request was to check a user
        isRequestCreateUser = NO;
    }


    if (response.isConflict) {
        //Error
        [self.userRegistrationVC.userNameError setHidden:NO];
        if ([self.userRegistrationVC.password canResignFirstResponder]) {
            NSLog(@"SI"); //This return NO
        }
        if ([self.userRegistrationVC canBecomeFirstResponder]) {
            NSLog(@"SI"); //This returns NO           
        }
        [self.userRegistrationVC.userName becomeFirstResponder]; 
    }

    if (response.isServerError) {
        //Error
        [self.userRegistrationVC.userNameError setHidden:NO];
        [self.userRegistrationVC.userName becomeFirstResponder];                
    }



    if (response.isOK) {
        //Success

        //Retirar la pantalla de registro de usuario
        [self.viewControllerToPresentModalView dismissModalViewControllerAnimated:YES];

        //Si la peticion fue crear un usuario
        if (isRequestCreateUser) {
            //Guardar el nombre de usuario en las preferencias del usuario
            [[NSUserDefaults standardUserDefaults] setValue:self.userName forKey:kNombreUsuario];           
            //Guardar la contraseña en KeyChain
             [SFHFKeychainUtils storeUsername:self.userName andPassword:self.password forServiceName:kServiceName updateExisting:YES error:nil];
        }

        [self.delegate AEMUserRegistrationSucess];
    }    
}

The sequence of calls will be:

  • -(void)registerUser
  • -(void)AEMUserRegistrationVCUserName:(NSString *)un password:(NSString *)pw
  • -(void)request:(RKRequest *)request didLoadResponse:(RKResponse *)response

Reading the Forum many questions are answered with [UITextField becomeFirstResponder] as a solution, so maybe I'm missing something important, and I cannot make it work.

The docs say you can override canBecomeFirstResponder to return YES, but how can I override a UITextField method? Is this what needs to be done?

David Casillas
  • 1,801
  • 1
  • 29
  • 57

1 Answers1

0

Try a UITextView. The pain of dealing with iPhone: Stop UIScrollView from auto scrolling when UITextView text is changed can become your fruit. It does the exact opposite, no matter what... annoying but maybe viable if you limit the input via delegate callbacks

Community
  • 1
  • 1
Stephen J
  • 2,367
  • 2
  • 25
  • 31