如果 UITextField 为空,则不会出现 UIAlert
If UITextField is blank UIAlert does not appear
我有一个包含 4 个 UITextField 的应用程序,我想做的是在 UITextfield 为空时显示一个 UIAlert。我有我选择显示 UIAlert 的功能, 如下:
func displayMyAlertMessage(userMessage: String) {
let myAlert = UIAlertController(title: "Alert", message: userMessage, preferredStyle: UIAlertController.Style.alert)
let okAction = UIAlertAction(title: "Ok", style: UIAlertAction.Style.default, handler: nil)
myAlert.addAction(okAction)
self.present(myAlert, animated: true, completion: nil)
}
我看过这个 但无法让它工作,因为我有 UIPickerView 来显示文本。下面是我为 didSelectRow 编写的代码:
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if TrackTextField.isFirstResponder {
selectedTrack = tracks[row]
guard let track = selectedTrack?.isEmpty else {
displayMyAlertMessage(userMessage: "You have to fill in a track")
return
}
TrackTextField.text = String(track)
} else if firstTextField.isFirstResponder {
firstDriver = drivers[row]
firstTextField.text = firstDriver
} else if secondTextField.isFirstResponder {
secondDriver = drivers[row]
secondTextField.text = secondDriver
} else if thirdTextField.isFirstResponder {
thirdDriver = drivers[row]
thirdTextField.text = thirdDriver
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "getPredictor" {
let predictorVC = segue.destination as! UINavigationController
let vc = predictorVC.topViewController as! PredictorTableViewController
self.grandPrix.append(GrandPrix(granPrix: TrackTextField.text!, firstDriver: firstTextField.text!, secondDriver: secondTextField.text!, thirdDriver: thirdTextField.text!))
vc.grandPrix = self.grandPrix
}
}
我已经使用 TrackTextField 来测试警报是否有效。当我点击 Grandprix Predictor 时,UIAlert 没有出现。下面是 segue 到下一个 UIViewController 的函数。
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "getPredictor" {
let predictorVC = segue.destination as! UINavigationController
let vc = predictorVC.topViewController as! PredictorTableViewController
self.grandPrix.append(GrandPrix(granPrix: TrackTextField.text!, firstDriver: firstTextField.text!, secondDriver: secondTextField.text!, thirdDriver: thirdTextField.text!))
vc.grandPrix = self.grandPrix
}
如有任何帮助,我们将不胜感激。
您的检查不应在选取器视图委托中完成。
答案真的取决于你想用什么来触发检查。您很可能想在用户应该填写所有四个文本字段后单击某个按钮时进行检查。
添加一个verifyInput
方法。类似于:
func verifyInput() -> Bool {
if TrackTextField.text?.isEmpty || firstTextField.text?.isEmpty || secondTextField.text?.isEmpty || thirdTextField.text?.isEmpty {
// at least one is empty, show your alert here
return false
} else {
return true // all good
}
}
然后从某个按钮处理程序或 shouldPerformSegue
调用 verifyInput
。同样,这取决于您要使用什么事件来触发检查。
示例:
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
// Only validate for this specific segue
if identifier == "getPredictor" {
return verifyInput()
} else {
return true
}
}
根据需要调整逻辑。
我有一个包含 4 个 UITextField 的应用程序,我想做的是在 UITextfield 为空时显示一个 UIAlert。我有我选择显示 UIAlert 的功能,
func displayMyAlertMessage(userMessage: String) {
let myAlert = UIAlertController(title: "Alert", message: userMessage, preferredStyle: UIAlertController.Style.alert)
let okAction = UIAlertAction(title: "Ok", style: UIAlertAction.Style.default, handler: nil)
myAlert.addAction(okAction)
self.present(myAlert, animated: true, completion: nil)
}
我看过这个
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if TrackTextField.isFirstResponder {
selectedTrack = tracks[row]
guard let track = selectedTrack?.isEmpty else {
displayMyAlertMessage(userMessage: "You have to fill in a track")
return
}
TrackTextField.text = String(track)
} else if firstTextField.isFirstResponder {
firstDriver = drivers[row]
firstTextField.text = firstDriver
} else if secondTextField.isFirstResponder {
secondDriver = drivers[row]
secondTextField.text = secondDriver
} else if thirdTextField.isFirstResponder {
thirdDriver = drivers[row]
thirdTextField.text = thirdDriver
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "getPredictor" {
let predictorVC = segue.destination as! UINavigationController
let vc = predictorVC.topViewController as! PredictorTableViewController
self.grandPrix.append(GrandPrix(granPrix: TrackTextField.text!, firstDriver: firstTextField.text!, secondDriver: secondTextField.text!, thirdDriver: thirdTextField.text!))
vc.grandPrix = self.grandPrix
}
}
我已经使用 TrackTextField 来测试警报是否有效。当我点击 Grandprix Predictor 时,UIAlert 没有出现。下面是 segue 到下一个 UIViewController 的函数。
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "getPredictor" {
let predictorVC = segue.destination as! UINavigationController
let vc = predictorVC.topViewController as! PredictorTableViewController
self.grandPrix.append(GrandPrix(granPrix: TrackTextField.text!, firstDriver: firstTextField.text!, secondDriver: secondTextField.text!, thirdDriver: thirdTextField.text!))
vc.grandPrix = self.grandPrix
}
如有任何帮助,我们将不胜感激。
您的检查不应在选取器视图委托中完成。
答案真的取决于你想用什么来触发检查。您很可能想在用户应该填写所有四个文本字段后单击某个按钮时进行检查。
添加一个verifyInput
方法。类似于:
func verifyInput() -> Bool {
if TrackTextField.text?.isEmpty || firstTextField.text?.isEmpty || secondTextField.text?.isEmpty || thirdTextField.text?.isEmpty {
// at least one is empty, show your alert here
return false
} else {
return true // all good
}
}
然后从某个按钮处理程序或 shouldPerformSegue
调用 verifyInput
。同样,这取决于您要使用什么事件来触发检查。
示例:
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
// Only validate for this specific segue
if identifier == "getPredictor" {
return verifyInput()
} else {
return true
}
}
根据需要调整逻辑。