[英]Programmatically focus on UITextView that is created as an AccessoryView
In my iOS app, I create a UIAlert and add a UITextView to it dynamically: 在我的iOS应用中,我创建一个UIAlert并向其中动态添加一个UITextView:
UIAlertView *tweetAlert = [[UIAlertView alloc] initWithTitle:@"Compose Tweet"
message:nil
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:nil];
tweetTextView = [UITextView new];
[tweetTextView setText:[NSString stringWithFormat:@"%@ %@", [[NSUserDefaults standardUserDefaults] valueForKey:@"tweetText"], self.setShareURL]];
[tweetAlert setValue:tweetTextView forKey:@"accessoryView"];
[tweetAlert addButtonWithTitle:@"Tweet"];
[tweetAlert show];
[tweetTextView becomeFirstResponder]; // focus textview and open keyboard
I attempt to focus on the UITextView in the last line so that the keyboard is open when the UIAlertView appears. 我试图将重点放在最后一行的UITextView上,以便在出现UIAlertView时打开键盘。 However, the keyboard does not appear, and the UITextView is not in focus.
但是,没有出现键盘,并且UITextView不在焦点上。
I also tried 我也试过
[[tweetAlert valueForKey:@"accessoryView"] becomeFirstResponder];
But I get the error 但是我得到了错误
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIAlertView 0x1358b3200> valueForUndefinedKey:]: this class is not key value coding-compliant for the key accessoryView.'
Any idea how to focus on the dynamically created UITextView, which is embedded as an accessoryView in a UIAlertView? 任何想法如何专注于动态创建的UITextView,该UITextView作为附件视图嵌入在UIAlertView中?
It is generally not recommended that you modify the view hierarchy of alert views. 通常不建议您修改警报视图的视图层次结构。 You should just use a text entry UIAlertView:
您应该只使用文本输入UIAlertView:
UIAlertView *tweetAlert = [[UIAlertView alloc] initWithTitle:@"Compose Tweet"
message:nil
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:nil];
tweetAlert.alertViewStyle = UIAlertViewStylePlainTextInput;
You can use the UIAlertViewDelegate
method - (void)didPresentAlertView:(UIAlertView *)alertView
to make the textview become first responder. 您可以使用
UIAlertViewDelegate
方法- (void)didPresentAlertView:(UIAlertView *)alertView
使textview成为第一响应者。
- (void)didPresentAlertView:(UIAlertView *)alertView {
[tweetTextView becomeFirstResponder];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.