iOS同步调用对话框 RunLoop的使用

更新时间: 2017-04-19 此种方案设计的弹出框, 不适用于
tableViewCell点击代理方法中操作; 但是可以通过在cell上增加button, 利用button的点击事件调用同步弹出框.
问题
iOS中弹窗显示后, 代码会继续执行, 然后使用
block获取用户操作结果; 但在开发中, 往往需要等待用户操作后再继续执行代码, 这时候我们可以借助RunLoop实现弹出窗的同步调用.
测试Demo
定义了一个
Bool的变量–isConfirm, 弹出对话框后, 打印isConfirm的值, 在用户操作后更改isConfirm的值, 查看打印结果和打印时机.
测试结果, 注意输出isConfirm的时机

代码
let alertVC = UIAlertController(title: "标题", message: "测试同步的弹窗", preferredStyle: .alert)
let cancel = UIAlertAction(title: "取消", style: .cancel) { (action) in
self.isConfirm = false
CFRunLoopStop(CFRunLoopGetCurrent())
}
let confirm = UIAlertAction(title: "确认", style: .default) { (action) in
self.isConfirm = true
CFRunLoopStop(CFRunLoopGetCurrent())
}
alertVC.addAction(cancel)
alertVC.addAction(confirm)
present(alertVC, animated: true, completion: nil)
CFRunLoopRun()
print("isConfirm---\(isConfirm)")
实现原理
在弹出对话框后手动启动RunLoop(
CFRunLoopRun()) 在用户操作后, 强行停止正在运行的RunLoop(CFRunLoopStop(CFRunLoopGetCurrent()))
写在最后
本人菜鸟一枚,欢迎大神前来指(tiao)教(xi), 带我进坑入门