Reputation: 3604
I am trying to restrict a UITextField's input to only alphanumeric characters ie. letters and numbers. I found a BOOL method implementation here on stack overflow: https://stackoverflow.com/a/16147160/3344977
So here is what I did with my code after reading that answer:
I added this property to my main file's header:
@property (nonatomic, strong) NSCharacterSet *charactersToBlock;
Added this to my viewDidLoad:
self.charactersToBlock = [[NSCharacterSet alphanumericCharacterSet] invertedSet];
Added the method implementation:
- (BOOL)textField:(UITextField *)field shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)characters
{
return ([characters rangeOfCharacterFromSet:self.charactersToBlock].location == NSNotFound);
}
And then I have tried calling the BOOL method in my viewDidLoad by starting to type this (_emailEntry is a UITextField property I have created in my view controller's header file):
[_emailEntry should
But xcode doesn't even show the option for our method of shouldChangeCharactersInRange
which makes me think that I might be calling the BOOL method incorrectly.
I am not sure how to implement this correctly. Thanks for the help.
Upvotes: 0
Views: 84
Reputation: 3604
I just realized that there is no need to call the BOOL method. All you have to do is add the method implementation code to your view controller and you're good to go.
Upvotes: 1