开发者

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

开发者 https://www.devze.com 2023-01-23 17:37 出处:网络
Is it possible to add Clear when editing starts to a text view? This option is available for the text field but I 开发者_JS百科couldn\'t find anything for the text view.

Is it possible to add Clear when editing starts to a text view? This option is available for the text field but I 开发者_JS百科couldn't find anything for the text view.

Thanks!


Implement the UITextViewDelegate method called textViewDidBeginEditing:, and inside of it, set the text property to an empty NSString object:

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


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

add notification in viewWillAppear:

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

method to clear the current text view

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

}

remove notification in viewWillDisappear:

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

Hope it helps somebody

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号