2017-06-23 87 views
0

我一直在嘗試獲取警報消息,當用戶觸摸「開始」以啓動計時器時,彈出一個警報消息,當任何或所有3個所需的文本框爲空時。模擬器中沒有提示消息彈出。警告消息未能出現

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

回答

0

的文本字段nil當it's空的,沒有文字,that's你爲什麼鴕鳥政策進入if語句。做這個檢查,而不是檢查您的文本是否爲""

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