NSSlider, NSStepper, and NSTextfield with Bindings: how to display initial value?

  • I have a slider, a stepper, and a textField that are synchronized using
    bindings. I didn't write any code to do this, just used Xcode with its
    Interface Builder. When the program starts I don't see the slider set to
    the "Current" position I'd like and the textField doesn't show the
    "Current" value either. Is there something I can do within Xcode or do I
    need to add a line of code somewhere (in a -WindowControllerDidLoadNib:
    somewhere, maybe)?

    If at all possible I'd like to do everything in Xcode without writing a
    line of code.
  • On May 28, 2013, at 13:16 , Paul Johnson <paul...> wrote:

    > I have a slider, a stepper, and a textField that are synchronized using
    > bindings. I didn't write any code to do this, just used Xcode with its
    > Interface Builder. When the program starts I don't see the slider set to
    > the "Current" position I'd like and the textField doesn't show the
    > "Current" value either. Is there something I can do within Xcode or do I
    > need to add a line of code somewhere (in a -WindowControllerDidLoadNib:
    > somewhere, maybe)?

    This usually happens because something in the keypath of the binding isn't KVO compliant.

    For example, if your controls are bound to "model.myValue" of File's Owner, and the "model" property doesn't get set until after the nib is loaded and it isn't set KVO compliantly, then your controls will see "myValue" as 0.

    If you can't spot a property that's obviously non-compliant, it's going to be necessary to take the key path apart property by property and verify that each is doing the right thing.

    Where is the initial value coming from? One of the values you set in the slider, stepper or text field in IB, or something else?
  • Quincy Morisses reply completely resolved the issue I was having with
    bindings. I realized that we exchanged emails without copying this board. I
    think Quincy's reply might be helpful to others, so I'm reposting his reply
    on this board:

    On May 28, 2013, at 16:25 , Paul Johnson <paul...> wrote:

    Thanks, Quincey, for your reply. AFAIK, my code is KVO-compliant. (I tried
    to use @property and in fact found a decent and recent example that taught
    me what I needed to synchronize the slider, stepper, and textField. Before
    I found this example I was getting some runtime errors about
    non-compliance. I started again from scratch, following the example code,
    and came up with a nice short sample program. I've still got the
    initialization issue though.)

    I think the best way to show what I've done is to just bundle the project
    in a .dmg file and send it to you as it is very short. I hope you can
    suggest something without using much of your time.

    When you bind the controls to a property, they get the initial value of the
    property -- the value of the control you set in IB is ignored when the
    binding is established.

    So you'll need to arrange for the property to be initialized to the correct
    value. For example, in the Bandwidth class:

    - (id) init {
    self = [super init];
    _densityEstimationBandwidth = 10;
    return self;
    }

    There is an alternative way to do this with literally no lines of code
    (though I it's probably going to be too simple-minded an approach when your
    app gets a bit more complicated). Select the Bandwidth object in your XIB,
    and display the identity inspector (3rd icon from the left). Add a
    user-defined run-time attribute, specifying "densityEstimationBandwidth"
    for the key path, "Number" for the type, and the desired initial value.

    On Tue, May 28, 2013 at 7:01 PM, Paul Johnson <paul...> wrote:

    > Wonderful! Thanks a lot! I owe you a beer.
    >
    > I learned a lot from your reply. Eventually I hope to fully understand the
    > intricacies of Bindings. I think a good book on this topic would be a
    > bestseller.
    >
    > I chose your 2nd method, setting the Key Path in the XIB file. (What I'm
    > doing is converting a program that uses (arghh) Qt, and I want to minimize
    > the amount of actual code I need to write so the advantage of using Xcode
    > and Objective-C is totally obvious.)
    >
    > Again, thanks for your help.
    >
    >
    > On Tue, May 28, 2013 at 6:48 PM, Quincey Morris <
    > <quinceymorris...> wrote:
    >
    >> On May 28, 2013, at 16:25 , Paul Johnson <paul...> wrote:
    >>
    >> Thanks, Quincey, for your reply. AFAIK, my code is KVO-compliant. (I
    >> tried to use @property and in fact found a decent and recent example that
    >> taught me what I needed to synchronize the slider, stepper, and textField.
    >> Before I found this example I was getting some runtime errors about
    >> non-compliance. I started again from scratch, following the example code,
    >> and came up with a nice short sample program. I've still got the
    >> initialization issue though.)
    >>
    >> I think the best way to show what I've done is to just bundle the project
    >> in a .dmg file and send it to you as it is very short. I hope you can
    >> suggest something without using much of your time.
    >>
    >>
    >> When you bind the controls to a property, they get the initial value of
    >> the property -- the value of the control you set in IB is ignored when the
    >> binding is established.
    >>
    >> So you'll need to arrange for the property to be initialized to the
    >> correct value. For example, in the Bandwidth class:
    >>
    >> - (id) init {
    >> self = [super init];
    >> _densityEstimationBandwidth = 10;
    >> return self;
    >> }
    >>
    >>
    >> There is an alternative way to do this with literally no lines of code
    >> (though I it's probably going to be too simple-minded an approach when your
    >> app gets a bit more complicated). Select the Bandwidth object in your XIB,
    >> and display the identity inspector (3rd icon from the left). Add a
    >> user-defined run-time attribute, specifying "densityEstimationBandwidth"
    >> for the key path, "Number" for the type, and the desired initial value.
    >>
    >>
    >
previous month may 2013 next month
MTWTFSS
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31    
Go to today