如何在UIAlertController的UIAlertAction处理程序中更改全局值(添加函数)?(How to change global value (add function) in the

编程入门 行业动态 更新时间:2024-10-28 21:15:01
如何在UIAlertController的UIAlertAction处理程序中更改全局值(添加函数)?(How to change global value (add function) in the handler of UIAlertAction of UIAlertController?)

我想表示警报,我做了。 但是有一个问题:我无法在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) } }

更多推荐

本文发布于:2023-07-22 19:19:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1222699.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:全局   函数   程序   如何在   UIAlertAction

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!