71632

Trouble Setting A Maximum Length For A Text Field

I'm having trouble setting a maximum length for the content of a text field. The process I took was implementing the UITextFieldDelegate in my header file. I then defined (and then called) this method:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSUInteger newLength = [textField.text length] + [string length] - range.length; return (newLength > 5) ? NO : YES; }

This was a method that I got by suggestion here on the site. I've also tried this:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSUInteger newLength = [zipCode.text length] + [string length] - range.length; return (newLength > 5) ? NO : YES; }

"zipCode" is the the outlet that I connected to the text field. Each time I've changed the code I've made sure to reconnect the outlet. I have a feeling it's a very simple thing that I'm missing. Apologies if this is a little convoluted, I have a lot to learn! Thanks.

Answer1:

Try this:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { return (zipCode.text.length + string.length <= 5); }

Then, in the viewDidLoad method:

- (void)viewDidLoad { zipCode.delegate = self; //..... [super viewDidLoad]; }

Recommend

  • Use of colon in variable declaration [duplicate]
  • want to know ever time while pressing on keyboard back button during textfield editing ios
  • shouldChangeCharactersInRange call twice
  • how much time does grid.py take to run?
  • SQL Regex get phone number
  • how to validate textfield that allows two digits after
  • Objective C Textfield Default Value “•••••••” to \"1•••••••”,“12•••••”,“123••••” when user cli
  • Manage UITextField's dynamic position using auto layout
  • UITextField — observe changes to selectedTextRange?
  • for loop iteration in django
  • php: best way to validate POST
  • Client server multithread Socket
  • How to move cursor from one text field to another automatically when the user clicks the Delete Butt
  • How to display keyboard in iOS8
  • NSFetchedResultsController with NSPredicate not updating
  • Post comment to WordPress Blog from iPhone programmatically
  • iOS Date formatting
  • Generate a unique string based on a pair of strings
  • Deleting a widget from QTableView
  • What is this iOS exception with NSCFType?
  • multidatatrigger with multibinding in ControlTemplate.Triggers
  • Is there a parser equivalent of 'fragment' marking in ANTLR4?
  • how to solve invalid conversion specifier warning in iphone app
  • Subclassing QGraphicsItem prevents me from being able to use itemAt() on a QGraphicsScene/View
  • Build Successful but not running on simulator
  • Why Encoding.ASCII != ASCIIEncoding.Default in C#?
  • one Local Olampyad Questions on Informatic in 2011
  • Google Custom Search with transparent background
  • Spring security and special characters
  • Trying to switch camera back to front but getting exception
  • Why winpcap requires both .lib and .dll to run?
  • Compare two NSDates in iPhone
  • KeystoneJS: Relationships in Admin UI not updating
  • Free memory of cv::Mat loaded using FileStorage API
  • How to set the response of a form post action to a iframe source?
  • Angular 2 constructor injection vs direct access
  • how does django model after text[] in postgresql [duplicate]
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • unknown Exception android
  • Programmatically clearing map cache
  • Net Present Value in Excel for Grouped Recurring CF