Thursday, November 27, 2008

NSStepper


A common use of NSStepper is to control the value displayed in an adjacent text field. Here, I want to use this method to control text size in a simplified fashion. In IB, I set the stepper's minimum and maximum values and increment (all as floats).

One trick to steppers is that you should control drag from the stepper to the Text Field and then select takeIntValueFrom. This results in an action being sent to the TF to reflect changes in the stepper. Here, the stepper has been pre-set to have the values 12.0 and 14.0 with an increment of 2.0. These values can be changed in code, by making the stepper an outlet of the AD.

I also set the TextField as an outlet of the AppDelegate and get the stepper's value in awakeFromNib. Otherwise, the value is not displayed until the stepper is clicked for the first time.

To find out when the stepper has been clicked, just control drag from the stepper to the App Delegate in IB, the same as for any control. That's it.

class PyStepperAppDelegate(NSObject):
myStepper = objc.IBOutlet()
myStepperTF = objc.IBOutlet()

def awakeFromNib(self):
self.myStepper.setMaxValue_(24.0)
self.myStepperTF.takeIntValueFrom_(self.myStepper)
self.fontSize = self.myStepperTF.intValue()
print 'fontSize', self.fontSize

@objc.IBAction
def stepperChanged_(self,sender):
self.myStepperTF.takeIntValueFrom_(self.myStepper)
self.fontSize = self.myStepperTF.intValue()
print 'stepperChanged_, 'fontSize', self.fontSize

No comments: