Simple Core Data Binding Question: NSTextField to Integer attribute
-
Chris Schmitt Simple Core Data Binding Question: NSTextField to Integer attribute Feb 22 2008, 06:03I am sure this is easy and I am just missing something, but I want to
bind my integer attribute to an NSTextField. The data is displayed
correctly, but after I update the value in the text field and try to
save the changes back to my model. I get an error saying that the
update failed trying to set the integer value to a string value.
I have been searching for a while and I think I need to use a number
formatter, but it would seem that there is an easier way to do this.
On a side note, what is the best way to make sure only numbers are
typed into the text field?
Thanks in advance -
On Feb 22, 2008, at 12:03 AM, Chris Schmitt wrote:> I am sure this is easy and I am just missing something, but I want
> to bind my integer attribute to an NSTextField. The data is
> displayed correctly, but after I update the value in the text field
> and try to save the changes back to my model. I get an error saying
> that the update failed trying to set the integer value to a string
> value.
>
> I have been searching for a while and I think I need to use a number
> formatter, but it would seem that there is an easier way to do this.
>
> On a side note, what is the best way to make sure only numbers are
> typed into the text field?
Using a number formatter is the easiest way to accomplish both of
these tasks. In Interface Builder, simply drag a NumberFormatter from
the Library pane onto your text field and voila, it's formatted as a
number, and it won't let the user enter anything but numbers into the
text field. When you select the text field in the IB, you'll see a
small $ icon which represents the NumberFormatter. Clicking on it and
going to the Attributes tab in the Inspector will let you configure
the formatter. -
On Feb 22, 2008, at 1:34 AM, Adam P Jenkins wrote:> On Feb 22, 2008, at 12:03 AM, Chris Schmitt wrote:
>
>> I am sure this is easy and I am just missing something, but I want
>> to bind my integer attribute to an NSTextField. The data is
>> displayed correctly, but after I update the value in the text field
>> and try to save the changes back to my model. I get an error
>> saying that the update failed trying to set the integer value to a
>> string value.
>>
>> I have been searching for a while and I think I need to use a
>> number formatter, but it would seem that there is an easier way to
>> do this.
>>
>> On a side note, what is the best way to make sure only numbers are
>> typed into the text field?
>
> Using a number formatter is the easiest way to accomplish both of
> these tasks.
The fundamental problem is that trying to set the value in the test
field is wrong.
<http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaBindings/Con
cepts/Troubleshooting.html#//apple_ref/doc/uid/TP40002148-182467>
mmalc -
On Feb 22, 2008, at 9:55 AM, mmalc crawford wrote:>> >
> On Feb 22, 2008, at 1:34 AM, Adam P Jenkins wrote:
>
>> On Feb 22, 2008, at 12:03 AM, Chris Schmitt wrote:
>>
>>> I am sure this is easy and I am just missing something, but I want
>>> to bind my integer attribute to an NSTextField. The data is
>>> displayed correctly, but after I update the value in the text
>>> field and try to save the changes back to my model. I get an
>>> error saying that the update failed trying to set the integer
>>> value to a string value.
>>>
>>> I have been searching for a while and I think I need to use a
>>> number formatter, but it would seem that there is an easier way to
>>> do this.
>>>
>>> On a side note, what is the best way to make sure only numbers are
>>> typed into the text field?
>>
>> Using a number formatter is the easiest way to accomplish both of
>> these tasks.
>
> The fundamental problem is that trying to set the value in the test
> field is wrong.
> <http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaBindings/Con
cepts/Troubleshooting.html#//apple_ref/doc/uid/TP40002148-182467
You're assuming that by "update the value in the text field", the
original poster meant that they were doing it programmatically.
However the post doesn't say that, so I assumed they meant they
updated the value by typing into it, in which case using a
NumberFormatter is a good solution to both prevent the user from
entering non-numeric input, and to convert between text and the
correct bound-to property type.
You're correct in pointing out that updating the text field's value
programmatically is the wrong way to go about things. However that
causes a different problem than what the original poster was describing.


