我有一个UITextField
。第一次单击时,我要以select all
编程方式输入文本。所以我调用[textField selectAll:nil]
了textFieldDidBeginEditing:
委托方法。下次单击时,我希望它成为普通的编辑模式。如何以编程方式实现此目标?
提前致谢。
当用户点击所选文本时,这可以防止出现弹出窗口。
要允许第一次点击始终选择所有文本,并取消第二次点击,请将您的当前代码保留在textFieldDidBeginEditing
方法中,并扩展UITextField
至override canPerformAction:withSender:
,以防止出现弹出窗口,如下所示:
UITextField子类
- (BOOL) canPerformAction:(SEL)action withSender:(id)sender {
/* Prevent action popovers from showing up */
if (action == @selector(paste:)
|| action == @selector(cut:)
|| action == @selector(copy:)
|| action == @selector(select:)
|| action == @selector(selectAll:)
|| action == @selector(delete:)
|| action == @selector(_define:)
|| action == @selector(_promptForReplace:)
|| action == @selector(_share:) )
{
//Get the current selection range
UITextRange *selectedRange = [self selectedTextRange];
//Range with cursor at the end, and selecting nothing
UITextRange *newRange = [self textRangeFromPosition:selectedRange.end toPosition:selectedRange.end];
//Set the new range
[self setSelectedTextRange:newRange];
return NO;
} else {
//Handle other actions?
}
return [super canPerformAction:action withSender:sender];
}
UITextFieldDelegate方法
//Select the text when text field receives focus
- (void) textFieldDidBeginEditing:(UITextField *)textField
{
[textField selectAll:nil];
}
//Hide the keyboard when the keyboard "Done" button is pressed
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return TRUE;
}
希望对您有所帮助!
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句