简体   繁体   English

ios UIAlertcontroller中的ios UITextView显示键盘

[英]ios UITextView in UIAlertcontroller show keyboard

I already set textView in UIAlertController like the sample code: 我已经像示例代码一样在UIAlertController中设置了textView:

-(void) addMessage:(UIBarButtonItem *)sender{

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"New message" message:nil preferredStyle:UIAlertControllerStyleAlert];

UIViewController *viewController = [[UIViewController alloc]init];
[viewController.view setBackgroundColor:[UIColor lightGrayColor]];

UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 8, 250, 30)];
label.text = @"Demo alert!!";
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor whiteColor];
[viewController.view addSubview:label];

UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(10, 35, 250, 60)];
[viewController.view addSubview:textView];

[alertController setValue:viewController forKey:@"contentViewController"];

UIAlertAction *ok = [UIAlertAction actionWithTitle:@"Submit" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

}];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

}];
[alertController addAction:ok];
[alertController addAction:cancel];


dispatch_async(dispatch_get_main_queue(), ^{

    [self presentViewController:alertController animated:true completion:nil];

});

} }

So,I want show keyboard when alertController present.What can I do? 所以,我想在alertController存在时显示键盘。我该怎么办?

Why do you use textView in AlertController? 为什么在AlertController中使用textView? It should be UITextField. 它应该是UITextField。 You can use method addTextFieldWithConfigurationHandler to add a textField to AlertController 您可以使用方法addTextFieldWithConfigurationHandler将textField添加到AlertController

In Swift like that: 在这样的Swift中:

    let alertController = UIAlertController(title: "title", message: nil, preferredStyle: UIAlertControllerStyle.Alert)
    alertController.addTextFieldWithConfigurationHandler { (nameTextField) in
        nameTextField.placeholder = "Enter Channel Name here ..."
        nameTextField.text = "name"
        nameTextField.tag = 1
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.textDidChange), name: UITextFieldTextDidChangeNotification, object: nameTextField)
        nameTextField.clearButtonMode = UITextFieldViewMode.WhileEditing
    }
    self.presentViewController(alertController, animated: true) {
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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