Home > UITextField > Limiting Text Field Input
Limiting Text Field Input
Posted on Wednesday, March 25, 2009 by android apps market for tablests
Received a question today about how to make a UITextField so that it can only accept a certain number of characters of input. Say, you wanted a text field that only allows up to five characters to be entered, and then accepts no more. It's actually easy.
First step is to set your controller (or other appropriate class) as the text field's delegate. You should conform that class to the UITextFieldDelegate protocol, and then do something like this:
After that, you just need to implement a delegate method to limit the input.
You can replace "5" with whatever value, variable, or constant is appropriate for your situation. Whenever we return NO from this delegate method, the pending edit is cancelled.
First step is to set your controller (or other appropriate class) as the text field's delegate. You should conform that class to the UITextFieldDelegate protocol, and then do something like this:
myTextField.delegate = selfYou can also set the delegate in Interface Builder by control-dragging from the text field to File's Owner or some other object instance, and selecting the delegate outlet.
After that, you just need to implement a delegate method to limit the input.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
return !([newString length] > 5);
}
You can replace "5" with whatever value, variable, or constant is appropriate for your situation. Whenever we return NO from this delegate method, the pending edit is cancelled.
Category Article iPhone SDK, UITextField
Powered by Blogger.
Blog Archive
-
▼
2009
(496)
-
▼
March
(36)
- Introducing Google Brain Search for mobile
- Speed with a Catch
- Apple Packaging
- Mobile internet usage and useful mobile ads
- WWDC First Time Guide
- WWDC Accommodations
- Wavefront OBJ Loader Open Sourced to Google Code
- Apple Store LA Book Sighting
- Differences in Delegation
- Icons for Multiple Developer Tool Installs
- NSConference
- Xcode Single Window Mode
- The Greatest Week of the Year
- WWDC Was Announced - June 8 - 12
- One Year In
- Limiting Text Field Input
- Look Ma, No Hands - Google Mobile App for Blackber...
- Updated to the Kotaku / Refund Clause Issue
- Kotaku and the Technicolor Contract Clause
- Rumor Mill
- Resuable Reusable Classes
- Guess Where These Were Taken�
- Magnifying Glass in a Text View inside a Table Vie...
- Image Processing on the iPhone
- A Freebie
- Version Control is Your Friend
- New YouTube App for Windows Mobile and Nokia S60 p...
- Something I CAN Tell You...
- Wish I Could Say More
- A Word of Caution about SDK 3.0
- On the fate of SQLitePersistentObjects�
- iPhone OS 3.0
- Particle Generator Bugfixes
- Here comes Google Voice
- New Image Search Results for Android and iPhone
- Finance for Android App
-
▼
March
(36)