0

I have a UITextField setup where user inputs currency. I want to apply formatting to the currency on the fly. eg. if user has entered 111 it should appear as 111. When user enters one more digit, app should be able to add , automatically and value should look like 1,111

I can format the NSString by converting it into NSNumber and then using NSNumberFormatter. However, I want to do it as user is typing and I need to know how to capture that event.

Shuki
  • 290
  • 2
  • 11
  • It's not the duplicate. The post you referred answers how to format, but not talks about the capture the event. May be I need to phase the question better. Thanks. – Shuki Nov 01 '13 at 04:05
  • Did you look at the accepted answer in the duplicate? It shows you how to do it. – rmaddy Nov 01 '13 at 04:06

2 Answers2

2

Found out the way to capture the event:

Create Target Action by Control + Drag from the text field (on storeyboard) into the controller.

Select Event as "Editing Changed" and you are good to go.

This target action is invoked every time keys are entered inside text box, and the logic to format can be applied here.

Could not upload screenshot because of lack of reputation points.

Shuki
  • 290
  • 2
  • 11
1
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string  
{
    if (textField == YourCurrencyTextField)
    {
      //Do your conversion Here
      return yourConverValue;
    }
else
    {
        return YES;
    }  
}

It will convert the input, while typing itself.. It is perfectly work for me..

TamilKing
  • 1,643
  • 1
  • 12
  • 23
  • Thanks for the reply, but was not clear on how and where you wire this method to. – Shuki Nov 01 '13 at 04:04
  • @Shuki:This was TextFielDelegate, so No need to call this method. if (textField == YourCurrencyTextField) this is your first textField inside the condition, you convert your textfield value and assign it to the second TextField – TamilKing Nov 01 '13 at 04:24