2017-06-23 1 views
0

J'ai essayé d'afficher un message d'alerte lorsqu'un utilisateur touche "Démarrer" pour démarrer une minuterie lorsque l'un ou l'ensemble des 3 champs de texte requis sont vides. Aucun message d'alerte n'apparaît dans le simulateur.Le message d'alerte ne s'affiche pas

@IBAction func start(_ sender: Any) { 
//the following code produces an alert if any of the specified text fields are blank when the "start" button is pressed 
if medName.text == nil || dose.text == nil || freq.text == nil { 

    let alertController = UIAlertController(title: "Field(s) must be filled in!", message: "Please complete all fields.", preferredStyle: UIAlertControllerStyle.alert) 

    alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) 
     in 
     self.dismiss(animated: true, completion: nil)})) 

    self.present(alertController, animated: true, completion: nil) 

    } else { 

     timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(SecondViewController.decreaseTimer), userInfo: nil, repeats: true) 

    let itemsObject = UserDefaults.standard.object(forKey: "cellContent") 

    var cellContent:[String] 

    if let tempItems = itemsObject as? [String] { 

     cellContent = tempItems 

     cellContent.append(medName.text!) 

    } else { 

     cellContent = [medName.text!] 

    } 

    UserDefaults.standard.set(cellContent, forKey: "cellContent") 
    //the following code clears the text within each of the specified text fields when start is pressed 
    medName.text = "" 
    dose.text = "" 
    freq.text = "" 
    } 
} 

Répondre

0

Un champ texte n'est pasnil quand vide et a une manière pas de texte, c'est pourquoi vous ne pas entrer dans le instruction if. Faites cette vérification à la place où vous vérifiez si votre texte est "":

if medName.text == "" || dose.text == "" || freq.text == ""