【问题标题】:selectall uitextfield does not always select allselectall uitextfield 并不总是全选
【发布时间】:2012-10-01 02:18:49
【问题描述】:
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    [textField selectAll:self];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;    
}

在上面,textField 选择正确,但是当我从键盘返回并连续第二次点击 textField 时,它没有选择文本。如果我没有连续选择它,或者我在从键盘返回之前取消选择文本,则该 textField 的下一个焦点会正确选择文本。

如何选择上述情况下的文本?

【问题讨论】:

    标签: iphone objective-c ios uitextfield selectall


    【解决方案1】:

    我找到了一个完美的解决方案(在下一个运行循环中调用 selectAll):

    - (void)textFieldDidBeginEditing:(UITextField *)textField
    {
        [textField performSelector:@selector(selectAll:) withObject:textField afterDelay:0.f];
    }
    

    【讨论】:

    • 谢谢,我会尽快测试一下
    • 你一定是个天才!这是 iOS 6 和 7 上的错误,我认为唯一的解决方案是提交错误,然后等待修复。谢谢!
    • 还是提交了一个错误:#15781101 "UITextField -selectAll: 只能每隔一段时间工作一次"。
    • 这似乎仍然是一个错误,因为我尝试了这两个:textField.selectAll(nil)textField.perform(#selector(UIResponder.selectAll(_:)), with: textField),而且都没有比其他时间更有效
    【解决方案2】:

    我使用 Grand Central Dispatch 解决了这个问题。您可以使用 dispatch_async 调用和 dispatch_get_main_queue() 作为第一个参数来包装 [textField selectAll:self];

        dispatch_async(dispatch_get_main_queue()){
            // ... code you want to run on the main queue goes here
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-02
      • 1970-01-01
      • 1970-01-01
      • 2019-07-24
      • 2019-05-06
      • 1970-01-01
      相关资源
      最近更新 更多