0

I have a UITextView and I can't seen to get the NSNotification of UITextViewTextDidBeginEditingNotification to work. I am implementing it almost the exact same as I implement the similar NSNotification for a UITextField. Here is my code to set up both notifications:

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

And I have implemented the method for the latter like so:

-(void)textViewDidBeginEditing:(UITextView *)textView {

Which I believe is correct, but when I click inside a UITextView, it crashes with the message

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteNotification text]: unrecognized selector sent to instance 0x7965e4d0'

If I change the end of my addObserver line so it says

...object:self];

then it doesn't crash but nothing happens (because I guess 'self' in this case is the View Controller

and if I change it to the name of the actual UITextView property like this it crashes

object:self.nameField];
Cocorico
  • 2,319
  • 3
  • 22
  • 31

2 Answers2

2

First of all, what is your field? is a UITextField or a UITextView?

If its a UITextField use:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidChange:) name:UITextFieldTextDidChangeNotification object:self.textField];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidBeginEditing:) name:UITextFieldTextDidBeginEditingNotification object:self.textField];

If its a UITextView then use:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewDidChange:) name:UITextViewTextDidChangeNotification object:self.textView];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewDidBeginEditing:) name:UITextViewTextDidBeginEditingNotification object:self.textView];

This is what you can do with Notification Center.

However I recommend to use the UITextFieldDelegate or UITextViewDelegate and implement its methods, dont forget to set the textView.delegate to self or to whoever is the delegate

Hazneliel
  • 531
  • 1
  • 5
  • 16
2

The error message says it all:

-[NSConcreteNotification text]: unrecognized selector sent to instance 0x7965e4d0

You are asking the notification for its text. I'm sure you meant to ask the text view for this property value instead.

Christian Schnorr
  • 10,768
  • 8
  • 48
  • 83