【问题标题】:How to create a switch expression with UITextFields in Swift 3如何在 Swift 3 中使用 UITextFields 创建开关表达式
【发布时间】:2016-11-05 04:36:06
【问题描述】:

我想检查我的UITextFields.text 状态。在!= "" 上执行一个函数并显示一个警报视图if == ""。因为我有 5 个不同的 UITextFields,所以我想到了一个 switch 声明。

@IBAction func doneButton(_ sender: Any) {

    let indexPath = IndexPath(row: 0, section: 0)
    let cell = self.tableView.cellForRow(at: indexPath) as! SetupCell

    var textFields: [UITextField] = []

    textFields = [cell.nameTF,
                  cell.townTF,
                  cell.birthdayTF,
                  cell.genreTF,
                  cell.deejayTF]

    for tf in textFields {

        print("called") // is printed

        if tf.text != "" {

            print("not empty") // is printed

            switch UITextField() {
            case cell.nameTF:
                saveCredential(ME, "name", cell.nameTF.text!)
            case cell.birthdayTF:
                saveCredential(ME, "birthday", cell.birthdayTF.text!)
            case cell.townTF:
                saveCredential(ME, "town", cell.townTF.text!)
            case cell.deejayTF:
                saveCredential(ME, "deejayName", cell.deejayTF.text!)
            case cell.genreTF:
                saveCredential(ME, "genre", cell.genreTF.text!)
            default:
                break
            }
        } else {

            print("is empty") // printed

            switch UITextField() {
            case cell.nameTF:
                presentAlertView(self, title: "Error", message: "Name missing")
            case cell.birthdayTF:
                presentAlertView(self, title: "Error", message: "Birthday missing")
            case cell.townTF:
                presentAlertView(self, title: "Error", message: "Town missing")
            case cell.deejayTF:
                presentAlertView(self, title: "Error", message: "Deejay name missing")
            case cell.genreTF:
                presentAlertView(self, title: "Error", message: "Genre missing")
            default:
                break
            }
        }
    }
}

但是switch 语句中的代码没有被执行。如果是空的,也不是,如果不是。我相信问题可能出在switch UITextField(),但我想不出解决方案。我错过了什么?非常感谢您的帮助。

PS。 saveCredentialpresentAlertView 在应用程序的其他部分完美运行,所以这可能不是问题。但可以肯定的是:

func presentAlertView(_ vc: UIViewController, title: String, message: String?) {
    var alert = UIAlertController(title: title, message: nil, preferredStyle: UIAlertControllerStyle.alert)
    if message != nil {
        alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
    }
    alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
    vc.present(alert, animated: true, completion: nil)
}

【问题讨论】:

    标签: ios swift switch-statement uitextfield swift3


    【解决方案1】:

    问题是您使用switch 语句传递UITextFiled 的新实例,而不是从Array 传递对象。

    switch tf {
    

    而不是

    switch UITextField() {
    

    【讨论】:

    • 谢谢你,成功了。会尽快接受
    • @DavidSeek 欢迎朋友 :)
    猜你喜欢
    • 2017-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-16
    • 2016-10-23
    • 1970-01-01
    • 2015-01-26
    • 1970-01-01
    相关资源
    最近更新 更多