简体   繁体   中英

UITextView's becomeFirstResponsder not working proprly

I have a UIView with a UITextView and UITextField as a subview. I want the keyboard to appear automatically on textFieldShouldReturn call.

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    [textView becomeFirstResponder];
    return YES;
}

but this does not work AND for some reason, it displays keyboard with cursor on next line in text view . I tried to set text nil in textview but it automatically call textDidChange method and cursor moves on new line without any text.

在此输入图像描述

This same technique works correctly for a UITextField and also it works fine with iOS 5.0 but how to fix this in iOS 4.3. Let me know if you have any alternative as well.

Am I missing something obvious?

There's a known bug in iOS with resigning and becoming first responder within the same runloop. Try the following

[textField resignFirstResponder];
[textView performSelector:@selector(becomeFirstResponder) withObject:nil afterDelay:0.0];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM