I have an application with 4 UITextFields and what I want to do is display a UIAlert if a UITextfield is blank. I have the function I have chosen to display the UIAlert which is as follows:
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)
}
I have looked at this example and could not get it to work as I have UIPickerView to display the text. Below is the code I for 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
}
}
I have used TrackTextField to test out if the alert works. As it happens when I click on Grandprix Predictor the UIAlert does not appear. Below is the function that segue's to the next 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
}
Any help would be appreciated.
Your check should not be done in the picker view delegate.
The answer really depends on what even you want to use to trigger the check. Most likely you want to do the check when some button is clicked after the user should have filled in all four text fields.
Add a verifyInput
method. Something like:
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
}
}
Then call verifyInput
from some button handler or perhaps shouldPerformSegue
. Again, it depends on what event you want to use to trigger the check.
Example:
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
// Only validate for this specific segue
if identifier == "getPredictor" {
return verifyInput()
} else {
return true
}
}
Adjust the logic as needed.