2016-08-01 2 views
0

Я уже установлен TextView в UIAlertController как образец кода:ИОС UITextView в UIAlertcontroller шоу клавиатуры

-(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 present.What я могу сделать?

+1

вызов «becomeFirstResponder» для UILabel когда обработчик завершения презентации UIAlertController называется. –

+0

это сработало !! Шон, большое вам спасибо ... –

ответ

0

Почему вы используете textView в AlertController? Это должен быть 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) { 
    }