cri*_*ald 15 cocoa-touch uitextfield uitextfielddelegate ios6
在iOS 6中,如果您在安全文本字段中键入文本,请更改为另一个文本字段,然后返回到安全文本字段并点击退格键,将删除所有字符.我对这种情况很好,但是,我试图根据此安全文本字段是否包含字符来启用/禁用按钮.我知道如何确定字段中的字符是什么,以及是否有命中退格但是我无法确定如何检测是否正在清除所有字符.
这是我用来获取字段的新文本的委托方法,但是,如果退格是,我似乎无法弄清楚如何获取新文本(假设新文本只是一个空白字符串)点击清除所有角色.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
//returns the "new text" of the field
NSString * text = [textField.text stringByReplacingCharactersInRange:range withString:string];
}
Run Code Online (Sandbox Code Playgroud)
任何帮助深表感谢.
谢谢!
Sas*_*ter 14
我用这个解决方案.删除char后,它不需要局部变量并正确设置光标位置.
这是这个解决方案的混搭:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (range.location > 0 && range.length == 1 && string.length == 0)
{
// Stores cursor position
UITextPosition *beginning = textField.beginningOfDocument;
UITextPosition *start = [textField positionFromPosition:beginning offset:range.location];
NSInteger cursorOffset = [textField offsetFromPosition:beginning toPosition:start] + string.length;
// Save the current text, in case iOS deletes the whole text
NSString *text = textField.text;
// Trigger deletion
[textField deleteBackward];
// iOS deleted the entire string
if (textField.text.length != text.length - 1)
{
textField.text = [text stringByReplacingCharactersInRange:range withString:string];
// Update cursor position
UITextPosition *newCursorPosition = [textField positionFromPosition:textField.beginningOfDocument offset:cursorOffset];
UITextRange *newSelectedRange = [textField textRangeFromPosition:newCursorPosition toPosition:newCursorPosition];
[textField setSelectedTextRange:newSelectedRange];
}
return NO;
}
return YES;
}
Run Code Online (Sandbox Code Playgroud)
最后想想看看如何确定退格何时清除安全UITextField的所有字符的任何人:
的UITextField:
self.passwordTextField
Run Code Online (Sandbox Code Playgroud)
私有属性(在init中初始化为NO - 可能不需要):
self.passwordFirstCharacterAfterDidBeginEditing
Run Code Online (Sandbox Code Playgroud)
UITextFieldDelegate方法:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
//if text is blank when first editing, then first delete is just a single space delete
if([textField.text length] == 0 && self.passwordFirstCharacterAfterDidBeginEditing)
self.passwordFirstCharacterAfterDidBeginEditing = NO;
//if text is present when first editing, the first delete will result in clearing the entire password, even after typing text
if([textField.text length] > 0 && self.passwordFirstCharacterAfterDidBeginEditing && [string length] == 0 && textField == self.passwordTextField)
{
NSLog(@"Deleting all characters");
self.passwordFirstCharacterAfterDidBeginEditing = NO;
}
return YES;
}
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
if(textField == self.passwordTextField)
{
self.passwordFirstCharacterAfterDidBeginEditing = YES;
}
}
Run Code Online (Sandbox Code Playgroud)
我希望这有助于某人,我也希望Apple只创建一个委托方法,当一个删除清除安全文本字段时调用该方法 - 这看起来很麻烦,但它确实有效.