I have a simple Parent Gateway for child friendly game using a UIAlertController.
When the user answers the question incorrectly - I DONT want the alert view to be dismissed, at the moment I have the device vibrate and the screen shake but I can't get the alert view to stay on the screen.
The answers provided are in Obj-C or involve disabling the button, which obviously won't work and the other answers do not work - so I was hoping there would be an updated answer.
func parentGate () {
var val1 = [1,2,3,4]
var val2 = [5,6,7,8]
let valIndex1 = Int(arc4random_uniform(UInt32(val1.count-1)))
let valIndex2 = Int(arc4random_uniform(UInt32(val2.count-1)))
accessAnswer = val1[valIndex1]+val2[valIndex2]
let alertController = UIAlertController (title: "Parental Control", message: "This section is for Parents only, \nplease answer the question below: \nWhat is \(val1[valIndex1]) + \(val2[valIndex2])?", preferredStyle: .alert)
let testAnswer = UIAlertAction(title: "OK", style: .default, handler: answerHandler)
let cancelAnswer = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alertController.addTextField(configurationHandler: answerTextFieldResult)
alertController.addAction(testAnswer)
alertController.addAction(cancelAnswer)
self.present(alertController, animated: true, completion: nil)
}
and then the testAnswer handler looks like this:
func answerHandler (alert: UIAlertAction) {
guard let suppliedAnswer = answerTextField?.text,
let answer = Int(suppliedAnswer) else {
return
}
if answer == accessAnswer {
print ("Good")
} else {
print ("Bad")
AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))
self.view.shake(count: 3, for: 0.3, withTranslation: 3)
}
}
If you want to see what the textField handler looks like at the moment:
func answerTextFieldResult(textfield: UITextField) {
answerTextField = textfield
answerTextField?.placeholder = "Answer"
}