如何在 UIAlertController 的 UIAlertAction 处理程序中更改全局值(添加函数)?
How to change global value (add function) in the handler of UIAlertAction of UIAlertController?
我想显示一个提示,我做到了。但有个问题:
我无法更改全局值或在 UIAlertController
的 UIAlertAction
的处理程序中添加函数。例如:
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)
我在UIAlertController
的UIAlertAction
的handler中添加了self.appDelegate.globalvalue = true
,但是值self.appDelegate.globalvalue
一直是false...self.appDelegate.globalvalue
没有改变说实话...
如何更改 UIAlertAction
或 UIAlertController
的处理程序中的值?或者我可以在单击警报上的“确定”后更改全局值吗?感谢你们所有人:)
如何获得对 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)
}
}
我想显示一个提示,我做到了。但有个问题:
我无法更改全局值或在 UIAlertController
的 UIAlertAction
的处理程序中添加函数。例如:
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)
我在UIAlertController
的UIAlertAction
的handler中添加了self.appDelegate.globalvalue = true
,但是值self.appDelegate.globalvalue
一直是false...self.appDelegate.globalvalue
没有改变说实话...
如何更改 UIAlertAction
或 UIAlertController
的处理程序中的值?或者我可以在单击警报上的“确定”后更改全局值吗?感谢你们所有人:)
如何获得对 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)
}
}