(Objective-c) Text view "Clear When editing starts"

Is it possible to add Clear when editing starts with a text view? This option is available for the text box, but I could not find anything for the text view.

Thank!

+5
source share
2 answers

Implement a method UITextViewDelegatecalled textViewDidBeginEditing:, and inside it set the property textto an empty NSStringobject:

- (void) textViewDidBeginEditing:(UITextView *) textView {
  [textView setText:@""];
  //other awesome stuff here...
}
+14
source

above did not work for me, had to use notifications:

add notification to viewWillAppear:

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textViewDidBeginEditing:) name:UITextViewTextDidBeginEditingNotification object:nil];

to clear the current text view

-(void)textViewDidBeginEditing:(NSNotification *)notif{
UITextView *textView=notif.object;
textView.text=@"";

}

delete notification in viewWillDisappear:

[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidBeginEditingNotification object:nil];

, -.

0

All Articles