59: Bindings, NSStepper, NSTextField

Problem: We want to use bindings to bind a couple of NSStepper and NSTextField to two variables within a model object.

Solution: Bind using an NSObjectController. We'll do the example in three stages. Stage 1 bind one pair of NSStepper and NSTextfield. Stage 2 add a button to examine the model's field, and another button with which to increment it. Stage 3 add the next pair

In Xcode create a new project and two classes: MyModel and MyObjectControler. Code them as shown.

// MyModel.h #import <Cocoa/Cocoa.h> @interface MyModel : NSObject { CGFloat floatValue; } @property CGFloat floatValue; @end // MyModel.m #import "MyModel.h" @implementation MyModel @synthesize floatValue; @end // MyObjectControler.h #import <Cocoa/Cocoa.h> #import "MyModel.h" @interface MyObjectControler : NSObjectController { IBOutlet id myModelObj; } @property (assign) IBOutlet id myModelObj; @end // MyObjectControler.m #import "MyObjectControler.h" @implementation MyObjectControler @synthesize myModelObj; @end

NSObjectController bindings

MyObjectController bindings

Go into IB by double clicking on MainMenu.xib. Drag an object and an NSObjectController onto the .xib window. In the Inspector name them MyMenu and MyObjectController respectively. Onto the application window panel drag a NSTextField and a NSStepper. Add a NSNumberFormatter to the text field.

Set the Number Formatter Attributes to minmum 0 and maximum 10000.

Set the Stepper Attributes Value to minmum 0 and maximum 10000 and tick the Auto Repeats box.

Under Control set the State to Enabled and Continuous.

Now for the binding. Click on MyObjectController, in the Bindings pane, under Controller Content tick the Bind box, choose MyModel from the pop-up and set Model Key Path to self. (Illustration above)

Next. Go to the Stepper binding pane and select Value. Tick the Bind checkbox and set the pop-up to MyObjectController. Controller key is Selection and into model key path type floatValue.

Do exactly the same thing for the NSTextField Bindings. Tick the Bind checkbox and set the pop-up to MyObjectController. Controller key is Selection and in the model key path select floatValue. One extra thing needs to be done. Check Continuously Updates Value.

Finally make MyObjectController a delegate of Application and link MyObjectController to MyModel. Save and run.

NSTextField NSStepper Bindings

NSTextField NSStepper bindings

If all went well your program should be running like a dream. Use the stepper to step, add a digit or two in the text field and the stepper will increment it.

Now we want to be able to modify the values from within the model and have this reflected in the stepper and textField values.

Add code to MyModel so that it now looks as follows

// MyModel.h #import <Cocoa/Cocoa.h> @interface MyModel : NSObject { CGFloat floatValue; } @property CGFloat floatValue; -(IBAction)showFieldValue:(id)pSender; -(IBAction)add1ToFieldValue:(id)pSender; @end // MyModel.m #import "MyModel.h" @implementation MyModel @synthesize floatValue; - (IBAction)showFieldValue:(id)pSender { NSLog(@"MyModel floatValue=%6.1f",self.floatValue); } // end showFieldValue -(IBAction)add1ToFieldValue:(id)pSender { self.floatValue += 1.0; NSLog(@"MyModel floatValue=%6.1f",self.floatValue); } // end add1ToFieldValue @end

Link up the connections to the buttons, save and run. Again hopefully you should be able to see the values in the Console and be able to increment it from the button and see the result in the text field.

Now lets add another field to the model and another pair of NSStepper and NSTextfield to the interface window.

This is what the MyModel code should now look like.

// MyModel.h #import <Cocoa/Cocoa.h> @interface MyModel : NSObject { CGFloat floatValue; CGFloat floatValue2; } @property CGFloat floatValue; @property CGFloat floatValue2; -(IBAction)showFieldValue:(id)pSender; -(IBAction)add1ToFieldValue:(id)pSender; @end // MyModel.m #import "MyModel.h" @implementation MyModel @synthesize floatValue; @synthesize floatValue2; - (IBAction)showFieldValue:(id)pSender { NSLog(@"MyModel floatValues 1=%6.1f 2=%6.1f",self.floatValue,self.floatValue2); } // end showFieldValue -(IBAction)add1ToFieldValue:(id)pSender { self.floatValue += 1.0; self.floatValue2 += 1.0; NSLog(@"MyModel floatValues 1=%6.1f 2=%6.1f",self.floatValue,self.floatValue2); } // end add1ToFieldValue @end

Go into Inteface Builder, add another NSTextField and an NSNumberFormatter as before and give them the same values as before. Add another NSStepper and give that the same valeus as before.

You only need to bind the new NSTextField and NSStepper. Do as per the earlier pair but this time type floatValue2 into the Model Key Path. Save and run and hopefully happy days.

Oh, almost forgot, a NSUserDefaultsController will magically appear in the xib window with the title Shared User Defaults Controller .

Panel with two NSTextField NSStepper Bindings

Panel with two NSTextField NSStepper bindings

If you want to download the code

Click the Download Link to obtain 059-NSStepper-NSTextField.zip file of all three of these OS X 10.6.1 Snow Leopard programs.

059-NSStepper-NSTextField.zip (7MB)



Please send me your comments

If you include your e-mail I may reply!  

Page last modified: 18:58 Sunday 12th. May 2013

Julius Guzy

Paintings & Drawings

  • painting after Leonardo Da Vinci's Battle of Anghiari

animatedPaint