Question

i'm developing a form. So when user click on submit button, i check if all uitextfield are filled:

-(BOOL)checkUITextField{
    BOOL flag = YES;
    if(cognome.text.length==0){
        flag=NO;
        cognome.backgroundColor=[UIColor redColor];
        cognome.text=@"CAMPO OBBLIGATORIO"; 
    }else if(indirizzo.text.length==0){
        flag=NO;
        indirizzo.backgroundColor=[UIColor redColor];
        indirizzo.text=@"CAMPO OBBLIGATORIO"; 
    }else if(citta.text.length==0){
        flag=NO;
        citta.backgroundColor=[UIColor redColor];
        citta.text=@"CAMPO OBBLIGATORIO"; 
    }
}

Now, if user click on one of the uitextfield with background red and string "CAMPO OBBLIGATORIO", i want to clear text and set back color to white. i try using uitextfielddelegate and implementing textFieldShouldBeginEditing checking this:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    if([textField.text isEqualToString:@"CAMPO OBBLIGATORIO"]){
        textField.text=@"";
        textField.backgroundColor=[UIColor whiteColor];
    }
}

Nothing happens..

so i try adding an ibaction and in xib adding it to every uitextfield but nothing happens :( What can i do?

Was it helpful?

Solution

I just mocked up a test using your logic and it should work just fine. My guess is that you have not assigned the UITextField delegate. In Interface Builder you need to ctrl+click on your UITextField and drag out to the object you want to act as delegate.

I would probably consider changing how you check if you need to clear the UITextField. Checking against a user interface string (@"CAMPO OBBLIGATORIO") will likely break if you i18nize your app later.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top