views:

135

answers:

2

I have the same initial problem as described in Integrate NSStepper with NSTextField:

I need to have a NSTextField working with a NSStepper as being one control so that I can edit an integer value either by changing it directly on the text field or using the stepper up/down arrows.

Using bindings as commented on by carlosb results in a usable UI. However, in the initial question carlosb describes the following:

Problem is that if I edit the text field then click the stepper again it will forget about the value I manually edited and use the stepper's internal value.

This is why I am posting a variation on this question again. carlosb's use of bindings doesn't solve this problem. This happens in both the current project I am working on and a test project I have created. The test project can be found at GitHub. You can download it there (even without git) via the "Download Source" button in the top right.

Is there a clean way to solve this issue?

A: 

Your problem is that that the editing isn't ended until you press return or the field loses focus (so the number is never actually changed). If you press return or leave the field after editing, you'll see it works as expected.

One solution is to check the "Continuously Updates Value" option in the text field's value binding and check the "Continuous" option in the text field's attributes.

This will make sure the value is being updated as it's typed, so an immediate click on the stepper will reflect these changes.

Joshua Nozzi
Thanks! I did try "Continuously Updates Value", but did not set the "Continuous" option. It works fine that way in the test app. In my current project the validate<key>:error: methods now get NSString parameters if the set<key>: method is called from the UI/bindings system. I will have to look into that. BTW: I just updated the test project.
JanX2
After trying a lot of combinations, this seems pretty hopeless. I can get the stepper issue to go away by using Joshua Nozzi's method. But as soon as I add either a formatter or validation via validate<key>:error: and the "Validates Immediately" option one or more pieces don't work anymore.
JanX2
I updated the test project yet again to demonstrate several approaches. The one that is working "best" is the "Continuous, Validates in Setter" method. The only remaining drawback is that the correct value is stored in the ivar, but the UI does not show the current value.
JanX2
A: 

Text fields and sliders are both in the view layer of the MVC pattern. You'll have much better results by binding both of those views to a property of your controller.

Peter Hosey
Yes. I do just that in the test project linked above. Joshua Nozzi's approach works fine in the basic case. It does not solve the issues that arrise when adding validation and/or a formatter.
JanX2