On Mar 4, 2011, at 03:40, Jonathan Taylor wrote:

> I have a window with a large number of editable text fields, and a big "Go" 
> button. The text fields are bound to values in my class. At the moment if 
> there is an edit of one of the values in progress and then I press "Go" then 
> the "old" value for that field is the one in my class variable that is acted 
> upon.
> 
> In my view it would make a lot more sense to accept the current field text, 
> probably while retaining the current selection in the edit session, and then 
> act on the "Go" command. Is there a compact way of doing this? I have seen 
> some rather convoluted code samples that accept the current edit session, but 
> retaining the current selection looks to be very complicated, and require 
> assumptions about the nature of the control involved. Surely there should be 
> a more general one-line way of achieving this? Or is there a good UI argument 
> why what I want is a bad idea?

I really don't really understand the part about "the current edit session", but 
in general the solution is fairly straightforward -- you make use of 
NSController and its implementation of the NSEditor protocol.

Instead of binding your text fields directly to your data model, bind them 
instead to a NSObjectController object in your NIB file, which is in turn bound 
to the data model. Then, in the action method of your Go button, first send one 
of the 2 'commitEditing...' methods to the NSObjectController. (One is 
synchronous and is used for reporting errors application-modally. The other is 
asynchronous and is used for reporting errors in a sheet on the offending 
window.) You'll also have to write code to deal with any text fields that 
refuse to accept their values, if that applies.

Once the commit is done, proceed with your action normally. This will use the 
committed text field values.

Nothing in any of this will or should have any effect on what's selected in the 
text field where editing was in progress. Why is retaining the current 
selection so complicated?


_______________________________________________

Cocoa-dev mailing list ([email protected])

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to