如何在UIAlertController中使用UITextView

我使用警报控制器创build了一个popup警报,并添加了两个警报操作(ok和cancel),如下所示。

UIAlertController * alert= [UIAlertController alertControllerWithTitle:@"Cycling" message:@"Please enter title and description" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { [alert dismissViewControllerAnimated:YES completion:nil]; }]; UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { [alert dismissViewControllerAnimated:YES completion:nil]; }]; [alert addAction:ok]; [alert addAction:cancel]; [self presentViewController:alert animated:YES completion:nil]; 

现在,我想添加UITextView。 因为我有两个文本字段,如标题和说明。 为了说明我想使用UITextView添加no.of行。 我试过我没有得到如何添加它。

请指教。

将UITextView添加到UIAlertController:

https://gist.github.com/bennagar/c0cd618bcd23c4c2dadf

 func showAlert() { let saveAction = UIAlertAction(title: "OK", style: .Default, handler: nil) saveAction.enabled = false let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil) alertController.view.addObserver(self, forKeyPath: "bounds", options: NSKeyValueObservingOptions.New, context: nil) NSNotificationCenter.defaultCenter().addObserverForName(UITextViewTextDidChangeNotification, object: textView, queue: NSOperationQueue.mainQueue()) { (notification) in saveAction.enabled = self.textView.text != "" } textView.backgroundColor = UIColor.greenColor() alertController.view.addSubview(self.textView) alertController.addAction(saveAction) alertController.addAction(cancelAction) self.presentViewController(alertController, animated: true, completion: nil) } override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) { if keyPath == "bounds"{ if let rect = (change?[NSKeyValueChangeNewKey] as? NSValue)?.CGRectValue(){ let margin:CGFloat = 8.0 textView.frame = CGRectMake(rect.origin.x + margin, rect.origin.y + margin, CGRectGetWidth(rect) - 2*margin, CGRectGetHeight(rect) / 2) textView.bounds = CGRectMake(rect.origin.x + margin, rect.origin.y + margin, CGRectGetWidth(rect) - 2*margin, CGRectGetHeight(rect) / 2) } } } 
 <script src="https://gist.github.com/bennagar/c0cd618bcd23c4c2dadf.js"></script> 

有了上述解决scheme我的textview没有正确定位。 这是我得到它的工作:

  // The \n is required so that the alertcontroller keeps space for the message. Add as many \n as you like your textview height to be self.alertController = [UIAlertController alertControllerWithTitle:@"Some title" message:@"\n\n\n\n\n\n\n\n" preferredStyle:UIAlertControllerStyleAlert]; self.alertController.view.autoresizesSubviews = YES; UITextView *textView = [[UITextView alloc] initWithFrame:CGRectZero]; textView.translatesAutoresizingMaskIntoConstraints = NO; textView.editable = NO; textView.dataDetectorTypes = UIDataDetectorTypeAll; textView.text = @"Some really long text here"; textView.userInteractionEnabled = YES; textView.backgroundColor = [UIColor clearColor]; // This will make the scroll view scrollable if the text is too long textView.scrollEnabled = YES; NSLayoutConstraint *leadConstraint = [NSLayoutConstraint constraintWithItem:self.alertController.view attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:textView attribute:NSLayoutAttributeLeading multiplier:1.0 constant:-8.0]; NSLayoutConstraint *trailConstraint = [NSLayoutConstraint constraintWithItem:self.alertController.view attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:textView attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:8.0]; NSLayoutConstraint *topConstraint = [NSLayoutConstraint constraintWithItem:self.alertController.view attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:textView attribute:NSLayoutAttributeTop multiplier:1.0 constant:-64.0]; NSLayoutConstraint *bottomConstraint = [NSLayoutConstraint constraintWithItem:self.alertController.view attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:textView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:64.0]; [self.retailerHelpAlertController.view addSubview:textView]; [NSLayoutConstraint activateConstraints:@[leadConstraint, trailConstraint, topConstraint, bottomConstraint]]; 

试试我从这里得到的代码: http : //useyourloaf.com/blog/2014/09/05/uialertcontroller-changes-in-ios-8.html

 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title", message:@"Message", preferredStyle:UIAlertControllerStyleAlert]; [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) { textField.placeholder = NSLocalizedString(@"LoginPlaceholder", @"Login"); }]; 

然后显示它。