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