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"
} 
4

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Okay thanks, I guess I will do a UI workaround that shows something like "Incorrect Answer"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.