Domanda

Is there a way to restrict a user to enter only certain information inside a UITextField? For example, I want to restrict a user to enter only U.S. states. I know I can use other controls, such as UIPickerView, but I want them to type this, if possible. Thanks.

È stato utile?

Soluzione

check out:

- ( BOOL )textField:( UITextField * )textField
    shouldChangeCharactersInRange:( NSRange )range
    replacementString:( NSString * )string

Here's an article on autocomplete given an array of possible values (an NSMutableArray). You could probably change it slightly to limit the entries instead of suggestions for autocomplete.

http://www.raywenderlich.com/336/how-to-auto-complete-with-custom-values

Hope that helps.

Altri suggerimenti

The UITextField restriction is in turn restricted to certain limits. You can restrict the text field to accept only the Alphabets, Alphanumeric values, or Numeric values. But its almost impossible to restrict text field from accepting State names of other countries but US.

One way you can do this is by having a dictionary/list of the State names in US. And when the user finished entering the text in the text field, match the entered text with the list you have. If the list doesn't contain the text typed, then the user is not genuine, and he is trying to cheat your app. :-0

The best way is to let the user pick from a UIPickerView or UITableView with the State names you want the user to pick from. By this way the user is completely restricted and no way he can cheat your app. ;-)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top