gpt4 book ai didi

ios - 如何在 UITextView textViewDidBeginEditing 中显示键盘后显示警报

转载 作者:行者123 更新时间:2023-11-28 07:38:17 25 4
gpt4 key购买 nike

在 textViewDidBeginEditing 中,我使用 UIAlertController 显示警报。警报显示在键盘之前(在模拟器上)。
如何在弹出警报之前显示键盘?

 func textViewDidBeginEditing(_ textView: UITextView) {

if self.balance <= 0 {
let alert = UIAlertController(title: "Balance Low", message: "Your balance is low.", preferredStyle: UIAlertControllerStyle.alert)

let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel) { (cancel) in
}

let okAction = UIAlertAction(title: "Buy", style: UIAlertActionStyle.default) { (action) in
self.segueToBuy()
}

alert.addAction(cancelAction)
alert.addAction(okAction)
self.present(alert, animated: true, completion: nil)

}

}

最佳答案

请使用 DispatchQueue.main.asyncAfter 在一定延迟后,每当用户在 UITextView 中键入文本时显示警报。

func asyncAfter(deadline: DispatchTime, qos: DispatchQoS = default, flags: DispatchWorkItemFlags = default, execute work: @escaping @convention(block) () -> Void)

Delcare 私有(private)实例变量,用于在本地显示警报。

var showAlert = true

textViewDidBeginEditing 中尝试下面显示的代码:

func textViewDidBeginEditing(_ textView: UITextView) {

if self.showAlert && self.balance <= 0 {
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
textView.endEditing(true)
let alert = UIAlertController(title: "Balance Low", message: "Your balance is low.", preferredStyle: UIAlertController.Style.alert)

let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel) { (cancel) in

self.showAlert = false
textView.becomeFirstResponder()
}

let okAction = UIAlertAction(title: "Buy", style: UIAlertAction.Style.default) { (action) in

textView.endEditing(true)
self.segueToBuy()
}

alert.addAction(cancelAction)
alert.addAction(okAction)
self.present(alert, animated: true, completion: nil)
}
}
}

关于ios - 如何在 UITextView textViewDidBeginEditing 中显示键盘后显示警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52878515/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com