【问题标题】:iOS UIAlertview with uitextview editableiOS UIAlertview 与 uitextview 可编辑
【发布时间】:2015-02-11 12:39:35
【问题描述】:

我正在寻找一种将可编辑的 UITextView 放入 UIAlertView 的方法。

我知道如何使用 :

放置一个简单的文本字段
alert.alertViewStyle = UIAlertViewStylePlainTextInput;

但这不是我想要的。我想要更大的文本输入,以便用户可以在新闻之后写评论。

这可能吗?

【问题讨论】:

  • 那么为什么要使用警报视图呢?您可以在 UITextView 中使用 uiview,只需在评论时将其添加为子视图。
  • 我已经在表格视图中有一个评论列表,我想从我的视图中取出“添加评论”以减少位置
  • 所以你可以添加一个 uiview 而不是显示 alertview,例如:[self.tableview addsubview:uiview]
  • 或者这个自定义控件可能会帮助你github.com/wimagguc/ios-custom-alertview
  • 不可能向UIAlertView 添加视图,因此实际上不可能使用UITextViewUIAlertView 获得您想要的。但是,您可以寻找自定义的 alertView。看看这个谷歌搜索google.co.uk/…

标签: ios uitextview uialertview


【解决方案1】:

不幸的是,UIAlertView 无法实现您所追求的目标。

Apple 不允许开发人员修改 UIAlertView 的视图层次结构或对其进行子类化。查看Apple Documentation for UIAlertView。在标记为子类化注释的部分下,您会发现

UIAlertView 类旨在按原样使用,不支持子类化。此类的视图层次结构是私有的,不得修改。

不幸的是,虽然UIAlertView 仍然有addSubview: 方法,所以它与Apple 实际告诉我们的内容相矛盾,但它仍然存在的原因是因为UIAlertViewUIView 的子类,它具有此方法。因此,Apple 所做的是他们已经覆盖了这个方法,因此它绝对不会做任何事情,所以当你调用 [myAlertView addSubview:myView]; 时不会做任何事情,并且不会将视图添加到 UIAlertView

因此,要获得您需要实现自定义 AlertView 之后的行为(查看 Google 搜索 Custom UIAlertView)。

幸运的是,Apple 在 iOS 8 中引入了一个名为 UIAlertController 的新类,可让您获得您所追求的行为,并且他们已弃用 UIAlertView 类。

【讨论】:

    【解决方案2】:

    在警报视图中添加自定义视图。

    根据要求将preferredStyle 更改为.alert 和.actionsheet。

    func showPopUpWithTextView() {
            let alertController = UIAlertController(title: "\n\n\n\n\n\n", message: nil, preferredStyle: .actionSheet)
    
            let margin:CGFloat = 8.0
            let rect = CGRect(x: margin, y: margin, width: alertController.view.bounds.size.width - margin * 4.0, height: 100.0)
            let textView = UITextView(frame: rect)
            textView.backgroundColor = .clear
    
            alertController.view.addSubview(textView)
    
            let submitAction = UIAlertAction(title: "Something", style: .default, handler: {(alert: UIAlertAction!) in print("Submit")
                print(textView.text)
            })
    
            let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: {(alert: UIAlertAction!) in print("cancel")})
    
            alertController.addAction(submitAction)
            alertController.addAction(cancelAction)
    
            self.present(alertController, animated: true, completion:{})
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多