如何使用PhoneNumberFormatter类在UITextField中格式化电话号码? [英] How to use PhoneNumberFormatter class to format phone number in UITextField?

查看:37
本文介绍了如何使用PhoneNumberFormatter类在UITextField中格式化电话号码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究有关尝试将UITextField自动格式化为电话号码的许多线程.我看到了指向Ahmed Abdelkader的PhoneNumberFormatter类的链接( http://the-lost-beauty.blogspot.com/2010/01/auto-formatting-phone-number.html )

I've been looking at a bunch of the threads here about trying to autoformat a UITextField into a phone number. I saw the links to Ahmed Abdelkader's PhoneNumberFormatter class ( http://the-lost-beauty.blogspot.com/2010/01/auto-formatting-phone-number.html )

但是我不知道如何实际实现它.我有两个要与该类一起使用的UITextField,其形式与许多其他UITextField用于其他目的.

But I don't understand how to actually implement it. I've got two UITextFields that I want to use with the class, in a form with many other UITextFields for other purposes.

艾哈迈德(Ahmed)的例子是:

Ahmed's example is:

UITextField *myTextField;
int myTextFieldSemaphore;
PhoneNumberFormatter *myPhoneNumberFormatter;
NSString *myLocale; //@"us"

//init semaphore
myTextFieldSemaphore = 0;

//bind events
[myTextField addTarget:self
                action:@selector(autoFormatTextField:)
      forControlEvents:UIControlEventEditingChanged
];

//handle events
- (void)autoFormatTextField:(id)sender {
  if(myTextFieldSemaphore) return;
 myTextFieldSemaphore = 1;
 myTextField.text = [phoneNumberFormatter format:myTextField.text withLocale:myLocale];
 myTextFieldSemaphore = 0;
}

我不太确定上面的代码在哪里,或者如何在属性中而不是他的变量中进行细分.到目前为止,我只能使文本字段拒绝输入:/-有没有人提供此类更完整实现的示例?

I'm not really sure where to put the above code, or how to sub in my properties instead of his variables. So far I've only been able to get the textfields to refuse input :/ - Has anyone got an example of a more complete implementation of this class?

推荐答案

结果证明,我能够将其连接到IB中,而无需使用该bind语句.很棒!唯一的问题是currentLocale建议与该类中的假设不符,因此我不得不编辑该类以使用en_US代替我们

Turns out I was able to hook it up in IB instead of using that bind statement. Works great! The only thing is that the currentLocale recommendation doesn't match with the assumptions in the class, so I had to edit the class to use en_US instead of us

这篇关于如何使用PhoneNumberFormatter类在UITextField中格式化电话号码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆