如何解决在文本字段下方显示错误消息,如 android
我尝试使用此链接:https://stackoverflow.com/a/56742493/13547105
但是每次当我按下后退按钮或保存按钮时,我的应用程序就会崩溃
我的错误是:原因:'无法删除变量:978:0x280750b90.marker{id:2509} colIndex:262 from engine
import UIKit
private var rightViews = NSMapTable<UITextField,UIView>(keyOptions: NSPointerFunctions.Options.weakMemory,valueOptions: NSPointerFunctions.Options.strongMemory)
private var errorViews = NSMapTable<UITextField,valueOptions: NSPointerFunctions.Options.strongMemory)
extension UITextField {
// Add/remove error message
func setError(_ string: String? = nil,show: Bool = true) {
if let rightView = rightView,rightView.tag != 999 {
rightViews.setobject(rightView,forKey: self)
}
// Remove message
guard string != nil else {
if let rightView = rightViews.object(forKey: self) {
self.rightView = rightView
rightViews.removeObject(forKey: self)
} else {
self.rightView = nil
}
if let errorView = errorViews.object(forKey: self) {
errorView.isHidden = true
errorViews.removeObject(forKey: self)
}
return
}
// Create container
let container = UIView()
container.translatesAutoresizingMaskIntoConstraints = false
// Create triangle
let triagle = Triangletop()
triagle.backgroundColor = .clear
triagle.translatesAutoresizingMaskIntoConstraints = false
container.addSubview(triagle)
// Create red line
let line = UIView()
line.backgroundColor = .red
line.translatesAutoresizingMaskIntoConstraints = false
container.addSubview(line)
// Create message
let label = UILabel()
label.text = string
label.textColor = .white
label.numberOfLines = 0
label.font = UIFont.systemFont(ofSize: 15)
label.backgroundColor = .black
label.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 250),for: .horizontal)
label.translatesAutoresizingMaskIntoConstraints = false
container.addSubview(label)
// Set constraints for triangle
triagle.heightAnchor.constraint(equalToConstant: 10).isActive = true
triagle.widthAnchor.constraint(equalToConstant: 15).isActive = true
triagle.topAnchor.constraint(equalTo: container.topAnchor,constant: -10).isActive = true
triagle.trailingAnchor.constraint(equalTo: container.trailingAnchor,constant: -15).isActive = true
// Set constraints for line
line.heightAnchor.constraint(equalToConstant: 3).isActive = true
line.topAnchor.constraint(equalTo: triagle.bottomAnchor,constant: 0).isActive = true
line.leadingAnchor.constraint(equalTo: container.leadingAnchor,constant: 0).isActive = true
line.trailingAnchor.constraint(equalTo: container.trailingAnchor,constant: 0).isActive = true
// Set constraints for label
label.topAnchor.constraint(equalTo: line.bottomAnchor,constant: 0).isActive = true
label.bottomAnchor.constraint(equalTo: container.bottomAnchor,constant: 0).isActive = true
label.leadingAnchor.constraint(equalTo: container.leadingAnchor,constant: 0).isActive = true
label.trailingAnchor.constraint(equalTo: container.trailingAnchor,constant: 0).isActive = true
if !show {
container.isHidden = true
}
// superview!.superview!.addSubview(container)
UIApplication.shared.keyWindow!.addSubview(container)
// Set constraints for container
container.widthAnchor.constraint(lessthanorEqualTo: superview!.widthAnchor,multiplier: 1).isActive = true
container.trailingAnchor.constraint(equalTo: superview!.trailingAnchor,constant: 0).isActive = true
container.topAnchor.constraint(equalTo: superview!.bottomAnchor,constant: 0).isActive = true
// Hide other error messages
let enumerator = errorViews.objectEnumerator()
while let view = enumerator!.nextObject() as! UIView? {
view.isHidden = true
}
// Add right button to textField
let errorButton = UIButton(type: .custom)
errorButton.tag = 999
errorButton.setimage(UIImage(named: "ic_error"),for: .normal)
errorButton.frame = CGRect(x: 0,y: 0,width: frame.size.height,height: frame.size.height)
errorButton.addTarget(self,action: #selector(errorAction),for: .touchUpInside)
rightView = errorButton
rightviewmode = .always
// Save view with error message
errorViews.setobject(container,forKey: self)
}
// Show error message
@IBAction
func errorAction(_ sender: Any) {
let errorButton = sender as! UIButton
let textField = errorButton.superview as! UITextField
let errorView = errorViews.object(forKey: textField)
if let errorView = errorView {
errorView.isHidden.toggle()
}
let enumerator = errorViews.objectEnumerator()
while let view = enumerator!.nextObject() as! UIView? {
if view != errorView {
view.isHidden = true
}
}
// Don't hide keyboard after click by icon
// UIViewController.isCatchTappedAround = false
}
}
class Triangletop: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func draw(_ rect: CGRect) {
guard let context = UIGraphicsGetCurrentContext() else {
return
}
context.beginPath()
context.move(to: CGPoint(x: (rect.maxX / 2.0),y: rect.minY))
context.addLine(to: CGPoint(x: rect.maxX,y: rect.maxY))
context.addLine(to: CGPoint(x: (rect.minX / 2.0),y: rect.maxY))
context.closePath()
context.setFillColor(UIColor.red.cgColor)
context.fillPath()
}
}
Viewcontroller.swift
import UIKit
class FormEditProfileInfoController: UIViewController,UITextFieldDelegate {
@IBOutlet weak var txtPOBox: UITextField!
override func viewDidLoad() {
txtPOBox.delegate = self
}
@IBAction func actionBack(_ sender: Any) {
dispatchQueue.main.async {
self.dismiss(animated: true,completion: nil)
}
}
@IBAction func actionSave(_ sender: Any) {
let poBox = txtPOBox.text ?? ""
if poBox.count <= 0 {
txtPOBox.setError("er_enter_po_Box".localizedstr(),show: true)
return false
}
txtPOBox.setError()
return true
}
func textField(_ textField: UITextField,shouldChangeCharactersIn range: NSRange,replacementString string: String) -> Bool {
textField.setError()
return true
}
}
感谢您的帮助
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。