在我的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
我尝试将焦点放在最后一行的UITextView上,以便在出现UIAlertView时打开键盘。但是,键盘不会出现,并且UITextView不在焦点上。
我也试过
[[tweetAlert valueForKey:@"accessoryView"] becomeFirstResponder];
但是我收到了错误
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIAlertView 0x1358b3200> valueForUndefinedKey:]: this class is not key value coding-compliant for the key accessoryView.'
知道如何关注动态创建的UITextView,它是作为UIAlertView中的accessoryView嵌入的吗?
答案 0 :(得分:1)
通常不建议您修改警报视图的视图层次结构。您应该只使用文本条目UIAlertView:
UIAlertView *tweetAlert = [[UIAlertView alloc] initWithTitle:@"Compose Tweet"
message:nil
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:nil];
tweetAlert.alertViewStyle = UIAlertViewStylePlainTextInput;
答案 1 :(得分:1)
您可以使用UIAlertViewDelegate
方法- (void)didPresentAlertView:(UIAlertView *)alertView
让textview成为第一响应者。
- (void)didPresentAlertView:(UIAlertView *)alertView {
[tweetTextView becomeFirstResponder];
}