How does an NSTextField know that it's value has changed?

  • I have a textfield that can either be edited by hand, or set through another control. (It's a page number, so you can jump to page 16 by typing in 16, or you can page up and down with the next/prev buttons) I have the value of the text field bound to an int page variable.

    It seems to work correctly, when you type a page number in, it jumps, when you page up and down, the page number in the text box changes appropriately.

    However, at some level, when I page up and down, the text box doesn't seem to know that it's value has changed, because if I type 1 it doesn't do anything. It doesn't call the bound set-method or anything. It looks like the textfield is thinking "I'm already 1, so I don't need to do anything." Even though it was 5 because I hit next 4 times.  If I type an number other than 1, it goes there. If I THEN type 1, it goes to page 1, because now it thinks it's on that other page.

    The problem seems to be that I'm changing the value of the textfield programatically, but some part isn't getting the message.

    Does this seem like a reasonable explanation? And either way, any ideas on what's causing it?

    thanks

    Brian Postow
    Senior Software Engineer
    Acordex Imaging Systems
  • On Tue, Apr 13, 2010 at 1:43 PM, Brian Postow <brian.postow...> wrote:
    > I have a textfield that can either be edited by hand, or set through another control. (It's a page number, so you can jump to page 16 by typing in 16, or you can page up and down with the next/prev buttons) I have the value of the text field bound to an int page variable.

    You shouldn't use one control to change the value of another control.
    Instead, both controls should change some property in your controller
    layer, which in turn informs all the controls to update themselves
    from the controller. Bindings simplifies this workflow.

    --Kyle Sluder
  • Hi Brian

    > The problem seems to be that I'm changing the value of the textfield programatically, but some part isn't getting the message.

    And this is your problem. If you change the value of a text field by typing into it, then that value is sent to the property on the model, but this is not true for programatic changes. The MVC pattern and bindings work when you change the value in the model programatically, not the text field. As long as the property in the model is KVO compliant, the text field will follow the changes to the property.

    Joanna

    --
    Joanna Carter
    Carter Consulting
  • Hi Brian

    > And this is your problem. If you change the value of a text field by typing into it, then that value is sent to the property on the model, but this is not true for programatic changes. The MVC pattern and bindings work when you change the value in the model programatically, not the text field. As long as the property in the model is KVO compliant, the text field will follow the changes to the property.

    In other words, connect both your up and down buttons to actions on the controller, in which you set the value of the property to which the text field is bound.

    Joanna

    --
    Joanna Carter
    Carter Consulting
  • On Apr 13, 2010, at 4:59 PM, Kyle Sluder wrote:

    > On Tue, Apr 13, 2010 at 1:43 PM, Brian Postow <brian.postow...> wrote:
    >> I have a textfield that can either be edited by hand, or set through another control. (It's a page number, so you can jump to page 16 by typing in 16, or you can page up and down with the next/prev buttons) I have the value of the text field bound to an int page variable.
    >
    > You shouldn't use one control to change the value of another control.
    > Instead, both controls should change some property in your controller
    > layer, which in turn informs all the controls to update themselves
    > from the controller. Bindings simplifies this workflow.

    Ok, so I should be calling my setPage: method from my next/previous code...

    Part of the problem was that the controller (where this code was) doesn't actually HAVE the page, that is in the document. So the page "property" is a ghost property, with a setPage: and a page method, but without any actual page instance variable... Thus, it didn't occur to me to set the page of the controller when I'm changing pages... When I do that, it works! thanks!

    Brian Postow
    Senior Software Engineer
    Acordex Imaging Systems
previous month april 2010 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    
Go to today