我正在 UIAlertController
和两个按钮中实现 UITextView
。在这里,我需要使用 Swift
验证基于 AlertviewController 按钮 Done
的 Textview empty
启用和禁用。
带有 TextView 的 AlertView
@IBAction func alertAction(_ sender: Any) {
let alert = UIAlertController(title: "Hello Alert!", message: "Write Your Text and Informations\n\n\n\n\n", preferredStyle: .alert)
alert.view.autoresizesSubviews = true
textView = UITextView(frame: CGRect.zero)
textView.translatesAutoresizingMaskIntoConstraints = false
textView.backgroundColor = UIColor.clear
textView.delegate = self
textView.text = "Placeholder text goes right here..."
textView.textColor = UIColor.lightGray
let leadConstraint = NSLayoutConstraint(item: alert.view, attribute: .leading, relatedBy: .equal, toItem: textView, attribute: .leading, multiplier: 1.0, constant: -8.0)
let trailConstraint = NSLayoutConstraint(item: alert.view, attribute: .trailing, relatedBy: .equal, toItem: textView, attribute: .trailing, multiplier: 1.0, constant: 8.0)
let topConstraint = NSLayoutConstraint(item: alert.view, attribute: .top, relatedBy: .equal, toItem: textView, attribute: .top, multiplier: 1.0, constant: -64.0)
let bottomConstraint = NSLayoutConstraint(item: alert.view, attribute: .bottom, relatedBy: .equal, toItem: textView, attribute: .bottom, multiplier: 1.0, constant: 64.0)
alert.view.addSubview(textView)
NSLayoutConstraint.activate([leadConstraint, trailConstraint, topConstraint, bottomConstraint])
alert.addAction(UIAlertAction(title: "Cancel", style: .default, handler: { action in
}))
alert.addAction(UIAlertAction(title: "Done", style: .default, handler: { action in
print("\(String(describing: self.textView.text))")
}))
present(alert, animated: true)
}
func textViewDidBeginEditing(_ textView: UITextView) {
if textView.textColor == UIColor.lightGray {
textView.text = ""
textView.textColor = UIColor.black
}
}
func textViewDidEndEditing(_ textView: UITextView) {
if textView.text == "" {
textView.text = "Placeholder text ..."
textView.textColor = UIColor.lightGray
}
}
最佳答案
UIAlertController
可以已经包含 UITextFields
。您可以根据 textField
是否为空来管理 UIAlertAction 的
isEnabled
状态。
1. 在您的 ViewController
中为 UIAlertAction
创建属性。
var doneAction: UIAlertAction?
2. 在 alertAction
按钮事件上,添加 textField
、cancelAction
和 doneAction
在上面创建的 alert
中。
@IBAction func alertAction(_ sender: Any) {
let alert = UIAlertController(title: "Hello Alert!", message: "Write Your Text and Informations\n\n\n\n\n", preferredStyle: .alert)
alert.view.autoresizesSubviews = true
//TextField...
let textView = UITextView(frame: CGRect.zero)
textView.translatesAutoresizingMaskIntoConstraints = false
textView.backgroundColor = UIColor.clear
textView.delegate = self
textView.textColor = UIColor.lightGray
textView.text = "Placeholder text goes right here..."
let leadConstraint = NSLayoutConstraint(item: alert.view, attribute: .leading, relatedBy: .equal, toItem: textView, attribute: .leading, multiplier: 1.0, constant: -8.0)
let trailConstraint = NSLayoutConstraint(item: alert.view, attribute: .trailing, relatedBy: .equal, toItem: textView, attribute: .trailing, multiplier: 1.0, constant: 8.0)
let topConstraint = NSLayoutConstraint(item: alert.view, attribute: .top, relatedBy: .equal, toItem: textView, attribute: .top, multiplier: 1.0, constant: -64.0)
let bottomConstraint = NSLayoutConstraint(item: alert.view, attribute: .bottom, relatedBy: .equal, toItem: textView, attribute: .bottom, multiplier: 1.0, constant: 64.0)
alert.view.addSubview(textView)
NSLayoutConstraint.activate([leadConstraint, trailConstraint, topConstraint, bottomConstraint])
//Done action...
let doneAction = UIAlertAction(title: "Done", style: .default) { (action) in
print("\(String(describing: alert.textFields?.first?.text))")
}
doneAction.isEnabled = false
alert.addAction(doneAction)
self.doneAction = doneAction
//Cancel action...
alert.addAction(UIAlertAction(title: "Cancel", style: .default, handler: nil))
self.present(alert, animated: true)
}
3. 现在,覆盖 UITextViewDelegate 的
textView(_:, shouldChangeTextIn:replacementText:)
方法,
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
let text = (textView.text! as NSString).replacingCharacters(in: range, with: text)
self.doneAction?.isEnabled = !text.isEmpty && text != "Placeholder text goes right here..."
textView.textColor = (!text.isEmpty && text != "Placeholder text goes right here...") ? .black : .lightGray
return true
}
func textViewDidBeginEditing(_ textView: UITextView) {
textView.text = nil
}
关于ios - 如何使用 Swift 根据 UITextView 文本长度启用和禁用 Alertaction 按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57766343/