UIAlertcontroller中的ios UITextView显示键盘

时间:2016-08-01 01:35:31

标签: ios uitextview uialertcontroller

我已经在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];

});

}

所以,我想在alertController出现时显示键盘。我该怎么办?

1 个答案:

答案 0 :(得分:0)

为什么在AlertController中使用textView? 它应该是UITextField。 您可以使用方法addTextFieldWithConfigurationHandler将textField添加到AlertController

在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) {
    }