设置错误文本时,材质文本字段崩溃

时间:2019-02-23 02:00:40

标签: swift cosmicmind

我正在使用CosmicMind库进行材料设计,并且尝试通过一些错误检查来设置基本的textfields,但是文档不是很好。

我已经设置了如下文本字段:

@IBOutlet weak var userNameField: ErrorTextField!

userNameField.placeholder = "Enter Username"
userNameField.delegate = self
userNameField.error = "Text is too long" // App Crashes here
userNameField.errorColor = Color.red.base

应用崩溃,EXC_BAD_ACCESS

我这样验证我的字段:

func textField(textField: TextField, didChange text: String?) {

    if textField == userNameField {

        if validateUsername(text: textField.text!) {
            userNameField.isErrorRevealed = true
        } else {
            userNameField.isErrorRevealed = false
        }
    }
}

即使我删除了该行,应用程序也会在userNameField.isErrorRevealed = true上崩溃。

1 个答案:

答案 0 :(得分:2)

我用您正在使用的相同库创建了以下代码,它们工作正常。我已经以编程方式创建了textField

import UIKit
import Material

class ViewController: UIViewController {

    fileprivate var emailField: ErrorTextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = Color.grey.lighten5

        emailField = ErrorTextField()
        emailField.placeholder = "Email"
        emailField.error = "Text is too long"
        emailField.delegate = self

        self.view.layout(emailField).height(40).width(200).centerVertically().centerHorizontally()
    }
}

extension ViewController: TextFieldDelegate {

    public func textFieldDidEndEditing(_ textField: UITextField) {
        (textField as? ErrorTextField)?.isErrorRevealed = false
    }

    public func textFieldShouldClear(_ textField: UITextField) -> Bool {
        (textField as? ErrorTextField)?.isErrorRevealed = false
        return true
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        (textField as? ErrorTextField)?.isErrorRevealed = true
        return true
    }
}

请参阅以下示例项目:Sample Project

我希望这会对您有所帮助。