How to handle when uitextview became first responder. I have text in text view and I want when the view became active want to clear the text. How can I do that? Thank开发者_运维百科s in advance
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
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.
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
}
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;
}
精彩评论