2016-11-05 3 views
0

Ich möchte meine UITextFields für ihre .text Status überprüfen. Führen Sie eine Funktion unter != "" aus und zeigen Sie eine Alarmansicht if == "" an. Da ich 5 verschiedene UITextFields habe, dachte ich an eine switch Aussage.Wie erstellt man einen Schalterausdruck mit UITextFields in Swift 3

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

Aber der Code innerhalb der switch Anweisung nicht ausgeführt wird. Weder der Fall, wenn leer, noch wenn nicht. Ich glaube, dass das Problem in switch UITextField() sein kann, aber ich kann eine Lösung nicht herausfinden. Was vermisse ich? Hilfe wird sehr geschätzt.

PS. Die saveCredential sowie die presentAlertView funktionieren perfekt auf anderen Teilen der App, so dass das nicht das Problem sein könnte. Aber sicher sein:

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

Antwort

1

Das Problem ist, dass Sie die neue Instanz von UITextFiled mit switch Aussagen sind vorbei, anstatt Objekt aus Array übergeben.

switch tf { 

Statt

switch UITextField() { 
+0

danke, dass es tat. wird so schnell wie möglich akzeptieren –

+0

@DavidSeek Willkommen Kumpel :) –

Verwandte Themen