Re: Triggering a notification?
Re: Triggering a notification?
- Subject: Re: Triggering a notification?
- From: "Michael Ash" <email@hidden>
- Date: Wed, 30 Jul 2008 21:58:14 -0400
On Wed, Jul 30, 2008 at 6:22 PM, Randall Meadows <email@hidden> wrote:
> On Jul 30, 2008, at 3:43 PM, Nick Zitzmann wrote:
>
>> On Jul 30, 2008, at 3:31 PM, Randall Meadows wrote:
>>
>>> Grrr, seems like I should know this, or at least be able to find out, but
>>> so far, no good.
>>>
>>> What is the trick to triggering the controlTextDidChange: delegate
>>> method/notification when you set the text of a NSTextField (for instance)
>>> *programmatically*?
>>
>> You can just do this manually, like this: (warning - written in Mail,
>> untested, use at your own risk, and all that)
>>
>> [someTextField setStringValue:@"Hello world"];
>> [[NSNotificationCenter defaultCenter]
>> postNotificationName:NSControlTextDidChangeNotification object:someTextField
>> userInfo:[NSDictionary dictionaryWithObject:[[someTextField window]
>> fieldEditor:YES forObject:someTextField] forKey:@"NSFieldEditor"]];
>
> Thanks; I had figured I could do this, and was actually in the process of
> writing this when you replied. I was thinking Cocoa had a more "elegant"
> manner to do this, rather than having to do it manually, but it works, so
> I'm happy.
The more elegant manner is to simply do whatever the notification
method does directly.
Generally notifications like this should be in response to user
actions. Triggering them in response to programmatic editing is
backwards. Generally changes should flow from the view to the model
when the user makes changes, and they should flow from the model to
the view when you make changes. So if you're changing the text field,
the guts that it edits should already have been updated before you
touched the field, requiring no further action.
Mike
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden