【问题标题】:Setting a UITextField into editing mode programmatically以编程方式将 UITextField 设置为编辑模式
【发布时间】:2010-11-09 05:08:36
【问题描述】:

我有一个UITextField,我想以编程方式将其设置为编辑模式(屏幕上的键盘和文本字段框中的光标)。我知道当这个视图出现在屏幕上时用户将处于编辑模式,所以我想使用户不必点击文本字段。

UITextField 的“编辑”属性是只读的 - 所以这不起作用。有没有办法以编程方式将UITextField 设置为编辑模式,在屏幕上使用键盘?

【问题讨论】:

    标签: iphone cocoa-touch


    【解决方案1】:

    在 UITextField 上调用 becomeFirstResponder

    相关问题: How do I show the keyboard by default in UITextView?

    【讨论】:

    • 我发现这是不可靠的,除非我确保在主线程中运行它:dispatch_async(dispatch_get_main_queue(), ^{[textView becomeFirstResponder];});
    【解决方案2】:

    您必须致电[textField becomeFirstResponder]

    【讨论】:

    • 您可能发布得太晚了,而且您没有得到任何支持,所以我决定给您应得的支持。 :)
    • 我给了你赞成票,因为使用了括号和分号。
    【解决方案3】:

    确实在 Obj-C 中调用 [textField becomeFirstResponder] 或在 Swift 中调用 textField.becomeFirstResponder()

    但是,请确保在 viewDidAppear 而不是 viewDidLoad 中调用它以防止出现奇怪的行为(请参阅:When set UITextField as FirstResponder programmatically, cause some weird actions on text editing)。

    【讨论】:

      猜你喜欢
      • 2010-12-10
      • 1970-01-01
      • 2019-12-06
      • 2010-09-17
      • 1970-01-01
      • 1970-01-01
      • 2013-04-23
      • 2014-09-05
      • 1970-01-01
      相关资源
      最近更新 更多