4 Replies Latest reply: Jan 28, 2009 11:37 AM by YuriNiyazov
YuriNiyazov Level 1 Level 1 (0 points)
I am working on a small Cocoa App; it's a very simple todo list consisting of an NSTableView, a NSTextField, and a NSButton "Add"

The "Add" button is initially disabled. The Add button is enabled/disabled depending on whether the NSTextField is empty or not. This is done by implementing the

- (void)controlTextDidChange:(NSNotification *)aNotification

method in the delegate for the textField.

After the Add button is clicked, the entry in the textfield gets added to the dataSource for the tableview, and the textfield gets blanked out:

{textField setStringValue:@""}

(replaced brackets with braces because the posting software turns that into strikethrough)

however, it is apparent that the delegate methods of the textField are not called when the contents are edited programmatically. Question: what is the appropriate way to get textField to call the delegate method? I would like for this call to follow the same path as a regular textField call. Here's one attempt:

{{textField delegate} controlTextDidChange:(NSNotification*)NSControlTextDidEndEditingNotification};

However, to be truly portable, I also have to check whether or not the delegate actually has the controlTextDidChange: selector (I know how to do that - won't include the code for now). Anyway - is this the correct and accepted way of doing this in Cocoa, or am I missing something more obvious and more elegant?

Message was edited by: YuriNiyazov

2.0ghz MacBook (Late 2008), Mac OS X (10.5.5)
  • 1. Re: Question about cocoa delegate messaging
    orangekay Level 5 Level 5 (4,085 points)
    Are you actually setting a delegate?
  • 2. Re: Question about cocoa delegate messaging
    YuriNiyazov Level 1 Level 1 (0 points)
    Yes, and the delegate methods are called as expected when the textField is modified by user input as opposed to by the program
  • 3. Re: Question about cocoa delegate messaging
    etresoft Level 7 Level 7 (24,270 points)
    YuriNiyazov wrote:
    The "Add" button is initially disabled. The Add button is enabled/disabled depending on whether the NSTextField is empty or not. This is done by implementing the

    - (void)controlTextDidChange:(NSNotification *)aNotification


    It might be easier to use key/value binding for this type of problem. It is like Core Data without the database.


    [textField setStringValue:@""]

    (replaced brackets with braces because the posting software turns that into strikethrough)


    Use two lines of

    with your code in between

    to fix that.

    however, it is apparent that the delegate methods of the textField are not called when the contents are edited programmatically.


    I would have to get knee deep in code to verify that. But if you are doing it programatically, why not just call the delegate yourself?


    Anyway - is this the correct and accepted way of doing this in Cocoa, or am I missing something more obvious and more elegant?


    I have one program where I have to do things like controlTextDidChange. It is a royal hassle. When I get time I plan to change it all to key/value binding. It is much easier to use.
  • 4. Re: Question about cocoa delegate messaging
    YuriNiyazov Level 1 Level 1 (0 points)
    You are absolutely right as far as the key/value binding goes. Binding the value of the textField to a property and binding the enabled/disabled state of the button to another property made everything significantly simpler