如何在UIAlertController中使用UITextView [英] How to use UITextView in UIAlertController

查看:1256
本文介绍了如何在UIAlertController中使用UITextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用警报控制器创建了一个弹出警报,并添加了两个警报操作(确定和取消),如下所示。

  UIAlertController * alert = [UIAlertController 
alertControllerWithTitle:@Cycling
message:@请输入标题和描述
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:




I created a popup alert using alert controller and added two alert actions(ok and cancel) as below.

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];

Now, i want add UITextView. Because I have two text field like title and description. For description i want to use UITextView for adding no.of lines. I tried i am not getting how to add it.

Please advice.

解决方案

Adding UITextView to 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>

Still locking for way to get the height of the marked view, when I have it I can just replace the /2 with the correct height.

这篇关于如何在UIAlertController中使用UITextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆