Discussion. Call this method when you want the object to be the first responder. Calling this method doesn't guarantee that the object becomes the first responder. UIKit asks the current first responder to resign as first responder, which it might not do.
becomeFirstResponder()Notifies the receiver that it's about to become first responder in its NSWindow .
You can definitely use a UITextViewDelegate
method of:
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
Just return YES and intercept inside that method. You can also do it for UITextFields
with UITextFieldDelegate
and:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
Hope that helps.
textViewShouldBeginEditing
actually triggers before the text view becomes the first responder.
textViewDidBeginEditing
will trigger once the text view becomes the first responder and would be the best place to execute code that needs to know what the active textview is.
If you are not really worried about which field is active and just want to clear the text once the field is tapped on you could use either function.
EDIT: The same methods are available for text fields.
The Swift 4 solution
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
return true
}
Previous answers do the job great for UITextBox, but if you have a custom class derived from NSResponder and need to know when it becomes first responder:
-(BOOL) becomeFirstResponder
{
// Your stuff here
return YES;
}
As above, you can override becomeFirstResponder but note that you must call the superclass implementation. If you don't, things like popping the keyboard on a text field won't work. i.e.
override func becomeFirstResponder() -> Bool {
if super.becomeFirstResponder() {
// set up the control state
return true
}
return false
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With