我有一个带有 4 个 UITextFields 的应用程序,如果 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 不会出现。下面是连接到下一个 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
}
任何帮助,将不胜感激。