initializing a cocoa IB binding

I have an NSTextField and an NSStepper that I'm trying to bind together. The stepper is setup to go from 0 to 100, and I'd like an initial value of 30.

For the stepper bindings, I have: value: bind to Object Controller, controller key = selection, model key path = edgeThreshold

For the stepper attributes, I have min=0, max=100, increment=1, current=30

I've got the text field set up in a similar fashion bindings, value: bind to Object Controller, controller key = selection, model key path = edgeThreshold

When I run the program, everything works as I'd expect. If I hit the stepper up, the text field increments. If I type in a new value, and then hit the stepper, the value adjusts from my typed in value.

The problem is that the initial value in the text field is nothing (empty), and the initial value of the state appears to be 0. If I hit the stepper after launch, the text field changes from nothing to 1. So I can't figure out where to put the 30, and where the state of this binding really lives. I thought it might be in the object controller, so I set its dictionary to have a keypath and value of "selection.edgeThreshold" and 30...but this did nothing as well.


You don't really need the object controller. You can just bind the values of both the text field and the stepper to the property, edgeThreshold. So, if that property was declared in the app delegate, they would be bound to App Delegate.edgethreshold. If you then set that property to 30 in your applicationWillFinishLaunching method (self.edgethreshold = 30;), the text field will show 30 when the app starts up and the stepper will be set to 30 as well (it doesn't matter what you set the starting value to be in IB, this will override it).


I found a youtube video which pretty much does exactly what I was trying to do:

http://www.youtube.com/watch?v=ESk6YLDtGR8

Rather than instantiate an Object Controller to handle the bindings, I gave my app controller a few properties, and then bound to it. In the app controller's -init method, I set my desired startup values.

链接地址: http://www.djcxy.com/p/85272.html

上一篇: 详细应用:绑定困难

下一篇: 初始化可可IB绑定