当用户触摸“开始”以启动计时器时,我一直尝试弹出警报消息,当任何或所有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 :(得分:0)
文本字段不是 nil
,当它为空且没有文本时,这就是您不输入if语句的原因。这样做会检查您的文本是""
:
if medName.text == "" || dose.text == "" || freq.text == ""