Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Question about cocoa delegate messaging

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)

Posted on Jan 27, 2009 5:58 PM

Reply
4 replies

Jan 28, 2009 10:56 AM in response to YuriNiyazov

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.

Question about cocoa delegate messaging

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.