我想在UIAlertController
中使用viewDidAppear()
显示提醒屏幕并等到按下该按钮(iOS 11)。
当前显示UIAlertController
时,警告屏幕会在viewDidAppear()
中调用,但不会显示,也无法点按此屏幕。
异步或延迟执行的警报屏幕。警报屏幕显示没有任何问题。
有没有什么好方法可以同步?
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
var doneloop = false
let alert = UIAlertController(title:"Title", message: "Message", preferredStyle: UIAlertControllerStyle.alert)
let action1 = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: {
(action: UIAlertAction!) in
print("push OK button")
doneloop = true // Runloop flag
})
alert.addAction(action1)
self.present(alert, animated: false, completion: nil)
while !doneloop {
RunLoop.current.run(until: Date(timeIntervalSinceNow: 0.1))
}
alert.dismiss(animated: false, completion: nil)
}
答案 0 :(得分:0)
我创建了这个方法,只需将完成处理所需的数据传递给此函数.....
//Alert with handling
func showMessageWithAction(_ messageText:String, messageTitle:String, okButtonTitle:String, cancelButtonTitle:String?, vc: UIViewController, completion:@escaping (Bool) -> Void) {
let attribute = NSMutableAttributedString.init(string: messageText)
attribute.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.white , range: NSRange(location: 0,length: messageText.count))
let alert = UIAlertController(title: messageTitle,
message: messageText,
preferredStyle:
UIAlertControllerStyle.alert)
alert.view.tintColor = UIColor(red: (0/255.0), green: (29/255.0), blue: (97/255.0), alpha: 1.0)
alert.addAction(UIAlertAction(title: okButtonTitle,
style: UIAlertActionStyle.default,
handler: {(alert: UIAlertAction!) in
completion(true)
}))
if let cancelButton = cancelButtonTitle {
alert.addAction(UIAlertAction(title: cancelButton,
style: UIAlertActionStyle.cancel,
handler: {(alert: UIAlertAction!) in
completion(false)
}))
}
vc.present(alert, animated: true, completion: nil)
}
答案 1 :(得分:0)
你这样做的方式相当混乱,没有必要同步它。
另外,我建议您在viewWillAppear
而不是viewDidAppear
中运行提醒演示文稿,以防您在每次进入此UIViewController
后都看到提醒。
如果您想要运行一次,请使用viewDidLoad
我已在UIAlertController
周围创建了一个包装器。
struct AlertWrapper {
static func presentAlert(for vc: UIViewController, with title: String, message: String, dismissAlertAction: UIAlertAction? = nil, applyAlertAction: UIAlertAction? = nil) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
if dismissAlertAction != nil {
alert.addAction(dismissAlertAction!)
}
if applyAlertAction != nil {
alert.addAction(applyAlertAction!)
}
vc.present(alert, animated: true, completion: nil)
}
}
上述方法中的示例
let applyAction = UIAlertAction(title: "Ok", style: .default) { (_) in
// What ever you would like to do after pressing 'Ok'
}
let dismissAction = UIAlertAction(title: "Cancel", style: .default) { (_) in
// What ever you would like to do after pressing 'Cancel'
}
AlertWrapper.presentAlert(for: self, with: "YOUR TITTLE", message: "YOUR MESSAGE", dismissAlertAction: dismissAction, applyAlertAction: applyAction)
可以随意提出任何问题,下次请阅读规则。
答案 2 :(得分:-1)
简单方法
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let alert = UIAlertController(title:"Title", message: "Message", preferredStyle: UIAlertControllerStyle.alert)
let action1 = UIAlertAction(title: "OK", style: UIAlertActionStyle.default)
alert.addAction(action1)
self.present(alert, animated: false, completion: nil)
}