I have two UITextFields in my app and the app has to do with currency. I am attempting to have it formatted correctly with the $ at the beginning and the decimal where it should. Like I said. I have been unable to find a noob answer to my question.
Whats happening here is you're adding an observer that tells us when the user is finished editing. Once that happens our formatCurrency
method does the work of updating the look of the textfield. The changes take full affect once the number field resigns its first responder status.
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(formatCurrency)
name:UITextFieldTextDidEndEditingNotification
object:self.numberField];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.numberField resignFirstResponder];
}
-(void)formatCurrency {
NSString *currentValue = self.numberField.text;
long long cur = [currentValue longLongValue];
NSNumber *curNum = @(cur);
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
self.numberField.text = [NSNumberFormatter localizedStringFromNumber:curNum
numberStyle:NSNumberFormatterCurrencyStyle];
}