3

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"
} 
Brewski
  • 654
  • 3
  • 14
  • 29
  • 1
    Possible duplicate of [Prevent UIAlertController to dismiss](https://stackoverflow.com/questions/28919670/prevent-uialertcontroller-to-dismiss) – Sandeep Bhandari Jun 12 '18 at 07:56
  • Possible duplicate of [Objective C - I would like UIAlertController to not be removed when button clicked](https://stackoverflow.com/questions/50158217/objective-c-i-would-like-uialertcontroller-to-not-be-removed-when-button-click) – Ivan Smetanin Jun 12 '18 at 07:58
  • It is not a duplicate, please read the question again, I don't need it in Objective C and I don't want to disable the action. – Brewski Jun 12 '18 at 08:03
  • @Brewski It's system behaviour - when you click on button alert closes and you can't prevent this. – Ivan Smetanin Jun 12 '18 at 08:17

1 Answers1

2

You can't do this. It's system behaviour - when you click on button alert closes and you can't prevent this.

Only one solution is to create a custom view controller that will look like native UIAlertController.

Ivan Smetanin
  • 1,999
  • 2
  • 21
  • 28