Как заставить UITextField выбрать все сначала, а затем редактировать программно?
У меня есть UITextField
, По первому клику хочу select all
текст программно. Я позвонил [textField selectAll:nil]
в textFieldDidBeginEditing:
метод делегата. Когда я делаю следующий щелчок, я хочу, чтобы он стал обычным режимом редактирования. Как реализовать это программно?
Заранее спасибо.
1 ответ
Решение
Это предотвращает появление всплывающего окна, когда пользователь нажимает на выделенный текст.
Чтобы при первом касании всегда выделяться весь текст, а во втором касании снимите флажок, сохраните текущий код в textFieldDidBeginEditing
метод и расширить UITextField
переопределить 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;
}
Надеюсь, это поможет!