selectall uitextfield并不总是选择全部

时间:2012-10-01 02:18:50

标签: iphone objective-c ios uitextfield selectall

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    [textField selectAll:self];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;    
}

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

如何选择上述案例中的文字?

2 个答案:

答案 0 :(得分:10)

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

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

答案 1 :(得分:2)

我使用Grand Central Dispatch解决了这个问题。您可以使用[textField selectAll:self];调用dispatch_asyncdispatch_get_main_queue()作为第一个参数进行换行。

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