我有一个简单的父网关,用于使用 UIAlertController 的儿童友好游戏。
当用户错误地回答问题时 - 我不希望解除警报视图,此时我让设备振动并且屏幕震动,但我无法获得警报视图以保持在屏幕上。
提供的答案在Obj-C 或涉及禁用按钮,这显然不会起作用,其他答案也不起作用 - 所以我当时希望有更新的答案。
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)
}
然后testAnswer处理程序如下所示:
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)
}
}
如果你想看看目前textField处理程序的样子:
func answerTextFieldResult(textfield: UITextField) {
answerTextField = textfield
answerTextField?.placeholder = "Answer"
}
答案 0 :(得分:2)
你不能这样做。 这是系统行为 - 当您点击按钮警报关闭时,您无法阻止此操作。
只有一种解决方案是创建一个看起来像本机UIAlertController
的自定义视图控制器。