Re: Stupid bindings!%#$% :)
Re: Stupid bindings!%#$% :)
- Subject: Re: Stupid bindings!%#$% :)
- From: Keary Suska <email@hidden>
- Date: Mon, 09 Jun 2014 08:42:49 -0600
On Jun 9, 2014, at 8:27 AM, William Squires wrote:
> Okay, clearly I'm still missing something. Let's say I have a model object:
>
> @interface SMPLPlayer : NSObject
>
> ...
> @property (nonatomic, assign) NSUInteger pcLevel;
> ...
> @end
>
> and now I also have an NSWindowController subclass:
>
> @interface SMPLRollPCWindowController : NSWindowController
>
> ...
> @property (nonatomic, weak) SMPLPlayer *thePC;
> @property (weak) IBOutlet NSTextField *playerLevelLabel; // <- Inserted by IB/Xcode
> ...
>
> @end
>
> I'm leaving off the majority of the "stuff" for simplicity here. In my SMPLRollPCWindowController.xib, I have a window with a label (that I tied to the above IBOutlet property in my window controller, via its File's Owner.) Now I want to bind the "value" of the label (an NSTextField) to the player object's pcLevel property. Unfortunately, I can't simply put in:
>
> Bind to: File's Owner
> Model Key Path: self.thePC.pcLevel
>
> because the "value" of the NSTextField is an NSString, while the player object's pcLevel property is an NSUInteger. I get a red octagon (in the Model Key Path field) if I try. Is this what value transformers are for? Or do I need an NSNumberFormatter on the NSTextField first?
Xcode's binding warning are just informational, although might tell you something useful. 1) Make sure Files Owner's class is set to SMPLRollPCWindowController; 2) "self" in the keypath is redundant and should be unnecessary. And yes, I would say as a general rule you should always attach an NSNumberFormatter to any NSTextField bound to a numeric value. Be warned however that you must also implement setNilValueForKey: because you will get an exception if the user clears the field.
> Along the same vein, if I implement my setter for pcLevel as:
>
> -(void)setPcLevel:(NSUInteger)value
> {
> if (value >= 1)
> {
> self.pcLevel = value;
> }
> else
> {
> // Does anything go here?
> }
> }
You do realize that the above is an infinite loop, don't you?
> and now, instead of a label, I use a TextField (i.e. the editable one) instead of the Label, what do I need to do to make sure the user can't type in invalid values for the pcLevel in the TextField (i.e. field-level validation), where the model "knows" what valid values are for the field? That is, is there some way to code the "else" branch above so the controller object can mediate this transfer-of-knowledge between the model and the view, without having to hard-code an NSNumberFormatter or some such into the view (which would introduce unwanted coupling)?
As a general rule, avoid side effects in your accessors. If you need simply range checking, allow the NSNumberFormatter to handle it. For other types of validation, use key-value validation.
HTH,
Keary Suska
Esoteritech, Inc.
"Demystifying technology for your home or business"
_______________________________________________
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