ios - 用计时器解除 UIAlertController
问题描述
UIAlertController.Style.actionSheet
如果没有点击 OK 按钮,几秒钟后关闭。
我尝试在提出actionSheet
调用方法以将其关闭后立即放置一个计时器,但它没有关闭它。
这是我的代码:
func showActionSheet(){
let alert = UIAlertController(title: "Some Tile", message: "Some Message", preferredStyle: UIAlertController.Style.actionSheet)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.cancel, handler: {
action in
self.closeSheetView()
}))
self.present(alert, animated: true, completion: nil)
timer = Timer.scheduledTimer(timeInterval: 2 , target: self, selector: #selector(self.closeSheetView), userInfo: nil, repeats: false)
}
@objc func closeSheetView(){
self.dismiss(animated: true, completion: nil)
}
仅供参考 - 该closeSheetView
方法似乎被调用,但工作表视图没有关闭。
解决方案
我只是在我的项目中做它没有计时器我使用延迟,也许它会为你工作......!
let alert = UIAlertController(title: "", message: "alert disappears after 5 seconds", preferredStyle: .alert)
self.present(alert, animated: true, completion: nil)
// here it work for 5 sec
let when = DispatchTime.now() + 5
DispatchQueue.main.asyncAfter(deadline: when){
// your code with delay
alert.dismiss(animated: true, completion: nil)
}
推荐阅读
- php - 为什么 Laravel 模型是静态的,我应该在自己的模型中使用静态函数?
- assembly - 如何仅使用 BIOS 中断在程序集引导加载程序中的硬盘驱动器上创建文件?
- python - 基于长度 matplotlib 箭袋的着色箭头
- oracle - 在 sqlplus 中设置换行符
- swift - Cocoa - 获取在动作之前有焦点的元素
- php - Doctrine ODM 唯一约束不验证属性
- angular - 从 finalize() 运算符发出另一个 Observable 是一种好习惯吗?
- python - 我可以用一个循环来解决这个问题,但为什么没有用这个递归函数显示数组的所有组合?
- spring - SCDF - 在不同的模式中创建元数据表并使用不同的模式处理批处理作业
- javascript - 将数组从平面一维转换为树,如 javascript/lodash