简体   繁体   English

以编程方式专注于作为AccessoryView创建的UITextView

[英]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.

相关问题 以编程方式专注于UITextView无法正常工作 - Programmatically focus on UITextView is not working 以编程方式创建时 UITextView 不滚动 - UITextView not scrolling when created programmatically 将UITextView或UILabel作为“ accessoryView”添加到UIAlertView? - Adding a UITextView or UILabel to UIAlertView as “accessoryView”? 如何以编程方式提供 UITextView 焦点? - How do I give a UITextView focus programmatically? 以编程方式创建的UITextView不再使用iOS 9滚动 - Programmatically created UITextView no longer scrolls with iOS 9 如何在iOS中为UITextView添加两个输入的abstractView? - How to add two input accessoryView for UITextView in iOS? 显示以编程方式创建的 UITextView/UITextField 的剪切/复制/粘贴菜单 - Display cut / copy / paste menu for UITextView / UITextField created programmatically 在 swift 中以编程方式创建的自定义 UITextView 未显示可点击链接 - Clickable link not displaying with programmatically created custom UITextView in swift UITextView无法在以编程方式创建的UITableView中关闭键盘 - UITextView can't dismiss the keyboard in programmatically-created UITableView 如何按方向更改在以编程方式创建的滚动视图中调整以编程方式创建的UILabel,UITextView,UITextfield的大小 - How to resize the programmatically created UILabel,UITextView,UITextfield in programmatically created scroll view as per orientation change
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM