4th Update: Finally solved. See post #4.
3rd Update: Partial improvement. See edit of next post.
2nd Update: Not actually fully resolved. See next post.
Update: I solved my problem. See edit below.
I have a cell-based TableView where one column has a TextFieldCell equipped with a NumberFormatter. The NumberFormatter is configured to only allow non-negative integers.
The problem I have is, when I type nonsense into the text field and then go to leave the text field, what I want to happen is for the nonsense to be replaced as if by atoi. So if I type 345abc I want it to be replaced with 345. (and if I type a negative number I want it to be replaced with 0, because Im disallowing negatives.)
But what actually happens is, if I type anything other than a positive integer, if there is even one non-numeric character, I cannot get out of the text field until I manually change it to an acceptable value. If I click outside the field, nothing happens. If I hit the tab key, nothing happens. If I hit the escape key, nothing happens.
I am appalled that Apple allowed such a brutally user-unfriendly behavior to be the default, but whatever, thats their decision to make. My question is, how do I fix it so it is user-friendly the way I want: if the user attempts to leave the TextFieldCell while it has a nonsense value, the text in the cell is parsed as if by atoi.
The TextFieldCell does not have a delegate, so I cannot simply use the TextFieldDelegateProtocol methods.
Ideas?
Edit: Solved. I made a subclasses of NSNumberFormatter to act as my custom integer formatter. In it I implemented (overrode) getObjectValue:forString:errorDescription: to call [string intValue], check for negative numbers and replace them with 0, then construct a new NSString from that number and call [super getObjectValue:etc.].
In IB I set the class of the NumberFormatter for the TextFieldCell to be my new subclass, and it works like a charm, except when the number format uses non-numeric characters.
3rd Update: Partial improvement. See edit of next post.
2nd Update: Not actually fully resolved. See next post.
Update: I solved my problem. See edit below.
I have a cell-based TableView where one column has a TextFieldCell equipped with a NumberFormatter. The NumberFormatter is configured to only allow non-negative integers.
The problem I have is, when I type nonsense into the text field and then go to leave the text field, what I want to happen is for the nonsense to be replaced as if by atoi. So if I type 345abc I want it to be replaced with 345. (and if I type a negative number I want it to be replaced with 0, because Im disallowing negatives.)
But what actually happens is, if I type anything other than a positive integer, if there is even one non-numeric character, I cannot get out of the text field until I manually change it to an acceptable value. If I click outside the field, nothing happens. If I hit the tab key, nothing happens. If I hit the escape key, nothing happens.
I am appalled that Apple allowed such a brutally user-unfriendly behavior to be the default, but whatever, thats their decision to make. My question is, how do I fix it so it is user-friendly the way I want: if the user attempts to leave the TextFieldCell while it has a nonsense value, the text in the cell is parsed as if by atoi.
The TextFieldCell does not have a delegate, so I cannot simply use the TextFieldDelegateProtocol methods.
Ideas?
Edit: Solved. I made a subclasses of NSNumberFormatter to act as my custom integer formatter. In it I implemented (overrode) getObjectValue:forString:errorDescription: to call [string intValue], check for negative numbers and replace them with 0, then construct a new NSString from that number and call [super getObjectValue:etc.].
In IB I set the class of the NumberFormatter for the TextFieldCell to be my new subclass, and it works like a charm, except when the number format uses non-numeric characters.
Last edited: