0

I am trying to connect an NSNumberFormatter to an NSTextfield using the formatter outlet of an NSTextField (shown in image below). My formatter subclass is as follows:

#import <Foundation/Foundation.h>

@interface FieldFormatter : NSNumberFormatter

@end

#import "FieldFormatter.h"

@implementation FieldFormatter

- (NSNumberFormatterBehavior)formatterBehavior {
    return NSNumberFormatterBehavior10_4;
}

- (NSNumberFormatterStyle)numberStyle {
    return NSNumberFormatterDecimalStyle;
}

@end

enter image description here

I get no errors when I run the program but the formatter is not working as it should. When I type a decimal into the text field it rounds up, i.e. 13.8 becomes 14. I can't input decimals at all into the text field.

I can get this to work correctly when using an NSTextField with NSNumberFormatter in Interface Builder. But I would prefer to do this through code so I can have more control over the format.

So what I'm trying to do is this - Subclass NSNumberFormatter, connect the formatter to text fields in the XIB, then after the user inputs a number into a text field that number is formatted.

Any suggestions would be helpful.

wigging
  • 8,492
  • 12
  • 75
  • 117
  • did you change the custom class of Field Formatter in IB? – Grady Player Oct 29 '12 at 03:20
  • @GradyPlayer The FieldFormatter is a subclass of NSNumberFormatter. The class of the object in IB is set to FieldFormatter. Is that what you were asking about? – wigging Oct 29 '12 at 12:53
  • I really don't understand how you can say it isn't working as it should if you haven't overridden any of the format methods... Perhaps you problem is in overriding the formatterStyle in code but not in IB, it may deserialze different keys based on that... But you really have an object that should behave just like the parent class – Grady Player Oct 29 '12 at 13:30
  • @GradyPlayer I'm completely lost on this and the documentation for NSNumberFormatter is not helpful. – wigging Oct 29 '12 at 13:34
  • @GradyPlayer Please see the last few lines of my updated question. – wigging Oct 29 '12 at 13:42
  • I think you have it all set up correctly, but you aren't doing any custom formatting, so I will ask in a different way: what formatting behavior are you trying to achieve? – Grady Player Oct 30 '12 at 03:39
  • @GradyPlayer I got it working using the `alloc & init` approach for the formatter (see answer below). – wigging Oct 30 '12 at 03:44

1 Answers1

2

Alloc init your formatter, and then just call setFormatter: on your text field.

FieldFormatter *formatter = [[FieldFormatter alloc] init];
[myTextField setFormatter:formatter];

And, if you're not doing anything with it in IB, you should get rid of that connection and the blue cube that represents your FieldFormatter instance.

rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • But if I get rid of the blue cube object how do I connect the text field to my FieldFormatter class? – wigging Oct 29 '12 at 12:55
  • Also, I've seen the `alloc init` examples for using NSNumberFormatter but none of them tell you where to actually put the code. I understand how to alloc & init the formatter and how to set parameters but where does the code belong? In a method, in a delegate, where? – wigging Oct 29 '12 at 13:50
  • @Gavin, The code where you alloc init your formatter should go in whatever class has the IBAction for your text field (don't forget to import FieldFormatter.h into that file), or you could just put it inside the applicationWillFinishLaunching method in the app delegate. As far as connecting it, that's what the setFormatter: method does, you don't need to do anything in IB other than have an IBOutlet for your text field. – rdelmar Oct 29 '12 at 15:10
  • Thanks, that answers my question for now. My next step will be to bind several text fields together to automatically update the other text fields based on the input of one text field. If this doesn't work for that then I'll post a separate question. – wigging Oct 29 '12 at 18:07