Material 文本域在设置错误文本时崩溃

Material Textfield crash on setting error text

我正在使用 CosmicMind 库进行 Material 设计,我正在尝试设置基本 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 上崩溃。

我使用您正在使用的相同库创建了以下代码,它们工作正常。我已经以编程方式创建了 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

希望对您有所帮助。