Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The behavior you are seeing does not depend on <code>textFieldShouldClear:</code>, whose default implementation already returns YES (<a href="http://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.html#//apple_ref/occ/intfm/UITextFieldDelegate/textFieldShouldClear%3a" rel="nofollow">source</a>): </p> <blockquote> <p>The text field calls this method in response to the user pressing the built-in clear button. (This button is not shown by default but can be enabled by changing the value in the clearButtonMode property of the text field.) This method is also called when editing begins and the clearsOnBeginEditing property of the text field is set to YES.</p> </blockquote> <p>The problem lays with <code>textField:shouldChangeCharactersInRange:</code> denying any change whenever the textField contains more that 8 characters:</p> <pre><code> if (textField.text.length &gt;=8) { return NO; // return NO to not change text </code></pre> <p>I don't know why you set this or if you could find another way to get the same, but if you want to leave it like this, then a possible workaround is checking the <code>replacementString</code> and if it is empty, allow the text change by returning YES.</p> <p>If you want a more sophisticated solution, you could think of setting an ivar flag when <code>textFieldShouldClear:</code> is called, so that when you find the flag set in <code>textField:shouldChangeCharactersInRange:</code>, you return YES.</p> <pre><code> - (BOOL)textFieldShouldClear:(UITextField *)textField { self.shouldClearTextCalled = YES; return YES; } - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { if (self.shouldClearTextCalled) return YES; self.shouldClearTextCalled = NO; if (textField.text.length &gt;=8) { return NO; // return NO to not change text } else { return YES; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload