2016-11-05 69 views
0

我想检查我的UITextFields.text状态。执行!= ""上的功能并显示警报视图if == ""。由于我有5个不同UITextFields我想到了switch声明。如何在Swift 3中使用UITextFields创建开关表达式

@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。 saveCredential以及presentAlertView完全适用于应用程序的其他部分,所以这可能不是问题。但可以肯定的:

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) 
} 

回答

1

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

switch tf { 

而不是

switch UITextField() { 
+0

谢谢你,做到了。将尽快接受 –

+0

@DavidSeek欢迎队友:) –