2016-11-05 35 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歡迎隊友:) –

相關問題