Ios 如何使UITextField先选择全部,然后以编程方式进行编辑?

Ios 如何使UITextField先选择全部,然后以编程方式进行编辑?,ios,objective-c,uitextfield,uitextfielddelegate,Ios,Objective C,Uitextfield,Uitextfielddelegate,我有一个UITextField。第一次单击时,我想按编程方式选择所有文本。所以我在textfielddbeginediting:delegate方法中调用了[textField selectAll:nil]。当我下一次单击时,我希望它成为正常编辑模式。如何通过编程实现这一点 提前感谢。这可以防止用户点击所选文本时出现弹出框。 要允许第一次点击始终选择所有文本,并取消第二次点击的选择,请将当前代码保留在textfieldDiBeginediting方法中,并扩展UITextField以覆盖canP

我有一个
UITextField
。第一次单击时,我想按编程方式选择所有文本。所以我在
textfielddbeginediting:
delegate方法中调用了
[textField selectAll:nil]
。当我下一次单击时,我希望它成为正常编辑模式。如何通过编程实现这一点


提前感谢。

这可以防止用户点击所选文本时出现弹出框。

要允许第一次点击始终选择所有文本,并取消第二次点击的选择,请将当前代码保留在
textfieldDiBeginediting
方法中,并扩展
UITextField
以覆盖
canPerformAction:Wissender:
,以防止弹出框出现,如下所示:

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];
}
//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;
}
UITextFieldDelegate方法

- (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];
}
//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;
}

我希望这有帮助

只是确认一下,到目前为止,需要两次点击,对吗?一次点击显示popover,然后另一次点击取消选择?是的,需要两次点击。但在选择全部时,不需要显示弹出窗口。它不起作用。第二次点击不会取消选择UITextField。您使用的是什么iOS版本?它在iOS 9上对我有效。我刚刚用一个新的取消选择实现更新了答案。我在iOS 9上的form scratch创建的项目中,在单视图模板中运行了这个测试。文本字段位于故事板的初始视图控制器中。如果这对你不起作用,请告诉我!它起作用了。但有一件事我发现,每当我点击中间的一个文本时,光标总是把最后一个字符(IE)带到最后。我需要长按并将光标移动到字符。是否要从用户点击的点查找插入符号位置?这可能会有帮助:
caretRectForPosition
的方法
uiteImport
。您可以从最后一个
UITouch