Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to know whether a UITextView has a focus or not

I know the becomeFirstResponder method to set a focus on a control, but how to know whether that control (for ex. UITextView) is currently having focus on it or not ?

EDIT: the problem is that I want (for example) to change the background color of the UITextView when it receives focus, where should I put the isFirstResponder call exactly ? should I use notifications in this case ?

thanks so much in advance.

like image 524
JAHelia Avatar asked Jul 03 '12 06:07

JAHelia


2 Answers

if([txtView isFirstResponder]){
     //Has Focus
} else {
     //Lost Focus
}

This is UITextView's delegate method,

- (BOOL)textViewShouldBeginEditing:(UITextView *)aTextView{
    //Has Focus
    return YES;
}

This is lost focus

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if ([text isEqualToString:@"\n"]){
        //Lost Focus
        [textView resignFirstResponder];
    }
    return YES;
}
like image 104
Mehul Mistri Avatar answered Nov 01 '22 04:11

Mehul Mistri


Use the UITextField delegate methods. When textField got focus the(bacame first responder)

- (void)textFieldDidBeginEditing:(UITextField *)textField; will be fired,

and when it lost focus

- (void)textFieldDidEndEditing:(UITextField *)textField; will be fired.

like image 6
Boris Nikolic Avatar answered Nov 01 '22 02:11

Boris Nikolic