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

Sheik_101

我有一个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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何选择列然后在GNU Emacs中进行编辑?

以编程方式进行换档选择

IOS - 目标C - 如何以编程方式删除的UITextField当选择pickerView要素?

eclipse插件:如何在编辑器中以编程方式选择文本?

jqGrid-如何在单元格编辑模式下以编程方式选择单元格?

以编程方式选择UITextField中的所有文本

如何以编程方式编辑 TextView 参数?

以编程方式在可内容编辑的HTML元素中选择文本?

以编程方式创建SubView,然后添加TapGesture以进行操作

如何在iOS Swift 3中以编程方式推送和呈现给UIViewController而不进行选择

Android:如何以编程方式选择一组视图并进行修改?

Highcharts 如何以编程方式放大?与您选择和区域进行视觉缩放时相同吗?

以编程方式编辑约会

如何以编程方式快速创建UITextField事件?

如何以编程方式选择 jQuery 滑块?

如何以编程方式保存选择?

如何以编程方式选择DbConfigurationType?

如何以编程方式取消选择ListViewItem?

如何以编程方式选择radlistview项目

如何以编程方式选择ListView项?

以编程方式 UITextField 标准样式

以编程方式触发UITextField的editingChanged

在选择进行编辑的 git rebase 中以交互方式删除提交

jQuery首先选择表中tr的td,然后遍历结果并将文本与值进行比较

如何以编程方式拒绝编辑页眉和页脚?

如何以编程方式设置编辑文本的可绘制背景

如何以编程方式清除Maya脚本编辑器?

如何以编程方式设置查看(编辑器)选项?

Ionic 2-如何以编程方式编辑Searchbar值