gpt4 book ai didi

swift - 警报消息无法出现

转载 作者:行者123 更新时间:2023-11-30 12:21:37 25 4
gpt4 key购买 nike

当用户触摸“开始”以启动计时器且 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 = ""
}
}

最佳答案

当文本字段为空并且没有文本时,它不是 nil,这就是您不输入 if 语句的原因。执行此检查,而不是检查文本是否为 "":

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

关于swift - 警报消息无法出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44727344/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com