我想表示警报,我做了。 但是有一个问题:我无法在UIAlertAction的UIAlertController的处理程序中更改全局值或添加函数。 例如:
let alertController = UIAlertController(title: "", message: "Do you want to leave ?", preferredStyle: UIAlertControllerStyle.Alert) let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil) let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: { action in self.appDelegate.globalvalue = true }) alertController.addAction(cancelAction) alertController.addAction(okAction) if let popoverController = alertController.popoverPresentationController { popoverController.sourceView = sender as! UIView popoverController.sourceRect = sender.bounds } self.presentViewController(alertController, animated: true, completion: nil)我在self.appDelegate.globalvalue = true的处理程序中添加self.appDelegate.globalvalue = true ,但值self.appDelegate.globalvalue始终为false ... self.appDelegate.globalvalue未更改为true ...
如何更改UIAlertAction的UIAlertController处理程序中的UIAlertController ? 或者我可以在警报上单击“确定”后更改全局值吗? 谢谢你们所有人:)
I would like to show a alert, and I did it. but There is a problem: I can not change a global value or add a function in the handler of UIAlertAction of UIAlertController. For example:
let alertController = UIAlertController(title: "", message: "Do you want to leave ?", preferredStyle: UIAlertControllerStyle.Alert) let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil) let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: { action in self.appDelegate.globalvalue = true }) alertController.addAction(cancelAction) alertController.addAction(okAction) if let popoverController = alertController.popoverPresentationController { popoverController.sourceView = sender as! UIView popoverController.sourceRect = sender.bounds } self.presentViewController(alertController, animated: true, completion: nil)I add self.appDelegate.globalvalue = true in the handler of UIAlertAction of UIAlertController, but the value self.appDelegate.globalvalue is always false... self.appDelegate.globalvalue is not changed to be true...
How can I do to change the value in the handler of UIAlertAction of UIAlertController? or Can I change a global value after clicking OK on the alert ? Thanks for all of yours :)
最满意答案
你如何得到AppDelegate的参考? 以下代码应该工作:
class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? var globalValue = false } class ViewController: UIViewController { var appDelegate: AppDelegate { return UIApplication.sharedApplication().delegate as! AppDelegate } override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated) let alert = UIAlertController(title: nil, message: "set global value to true", preferredStyle: .Alert) let action = UIAlertAction(title: "ok", style: .Default) { (action) in self.appDelegate.globalValue = true print("after: \(self.appDelegate.globalValue)") } alert.addAction(action) print("before: \(self.appDelegate.globalValue)") presentViewController(alert, animated: true, completion: nil) } }how do you get a reference to the AppDelegate? the following code should work:
class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? var globalValue = false } class ViewController: UIViewController { var appDelegate: AppDelegate { return UIApplication.sharedApplication().delegate as! AppDelegate } override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated) let alert = UIAlertController(title: nil, message: "set global value to true", preferredStyle: .Alert) let action = UIAlertAction(title: "ok", style: .Default) { (action) in self.appDelegate.globalValue = true print("after: \(self.appDelegate.globalValue)") } alert.addAction(action) print("before: \(self.appDelegate.globalValue)") presentViewController(alert, animated: true, completion: nil) } }更多推荐
发布评论