0

I subclassed NSTextField and overwrite the textDidChange: as:

- (void)textDidChange:(NSNotification *)notification
{
    // ... My own operation
}

But when I drag a input text box into my .xib file and control drag another class to assign the delegate I found the delegate's controlTextDidChange: method was never called.

Now tryingto solve this problem, I tried two ways al below:

I. calling super:

- (void)textDidChange:(NSNotification *)notification
{
    // ... My own operation
    [super textDidChange:notification];
}

But I got an error in runtime: attempt to insert nil object from objects[0]

II. calling delegate's method

- (void)textDidChange:(NSNotification *)notification
{
    // ... My own operation
    if ([self.delegate responseToSelector:@selector(controlTextDidChange:)])
    {
        [self.delegate ...];    // <--- Opps, something not happerned here.
    }
}

What not happerned? I expected that the auto-complete should display the controlTextDidChange: method at the position of ... above. But it did not, actually. I typed the method directly, compilation fail because method not found.

How should I make my sub-class call the delegate normally? How should I overwrite the textDidChange: method correctly?


Further question for Vytautas:

  1. I am sure I was using NSTextField. And I set a break point inside controlTextDidChange: method. As it was called, I should have known.
  2. I did control-drag the text field to the delegate object, and I print delegate object in the textDidChange: method, it was sure that the delegate was set correctly.
  3. The other delegate methods, such as controlTextDidBeginEditing: were called correctly. But controlTextDidChange: not called
  4. I tried comment out the over-written in the subclassed NSTextField class, then controlTextDidChange: was called.
  5. Therefore I was quite sure that I am not overwritting the textDidChange: right. But I do not known how to fix it.
  6. What made me confused mostly was that why auto-completion did not show the controlTextDidChange: method when I attempted to call it.

About the auto-completion, here is how it showed:

No - controlTextDidChange: method.


2nd further reply for Vytautas:

I tried calling '[self controlTextDidChange]' but it did not work, and error occurred (as highlighted below):

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Andrew Chang
  • 1,289
  • 1
  • 18
  • 38
  • Did you check that the delegate declared any necessary delegate protocol ? – uchuugaka May 16 '13 at 09:00
  • Yes. I am quite sure that does no matter with the delegate. The auto completion fill the `controlTextDidChange` for me in the `responseToSelector:` method but I could not directly call it like format of `[self.delegate controlTextDidChange:notification];`. – Andrew Chang May 17 '13 at 03:12

1 Answers1

1

I can say that - controlTextDidChange: is called for sure.

Maybe there is something wrong with you bindings in your *.xib.

Also it can be that in *.xib you are using NSTextView, not NSTextField.

In this case - controlTextDidChange: won't be called for sure. If that is the case then you should take a look to NSTextView, NSTextViewDelegate and NSTextDelegate. NSTextView delegate has an alternative method for this - textDidChange:

Vytautas
  • 573
  • 3
  • 8
  • I add some futrher description in my question text. – Andrew Chang May 16 '13 at 02:10
  • So, you are overwritting `-textDidChange:` of NSTextField, then take a look here: [`[NSControl controlTextDidChange:]`](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSControl_Class/Reference/Reference.html#//apple_ref/occ/instm/NSObject/controlTextDidChange:), there is some magic behind. As I remember, just call `[self controlTextDidChange:]` in your `[NSTextField textDidChange:]` implementation, and as parameter use same notification, which you have received. – Vytautas May 16 '13 at 12:16
  • It did not work as I posted in the question text. However, I found the solution: I could call `[self.delegate performSelector:@selector(controlTextDidChange:) withObject:notification];` and the delegate receive that successfully. – Andrew Chang May 17 '13 at 02:57
  • I will try to implement it by my self and I will let you know my solution – Vytautas May 17 '13 at 08:58
  • OK! It seemed that the `performSelector` is not a standard way. – Andrew Chang May 17 '13 at 13:27