【发布时间】:2015-12-10 07:54:27
【问题描述】:
我想编写一个函数alert() 并运行它。但我想在任何控制器中显示此警报而不重复代码。
例如:我有 Presence.swift 类,在这里我有一些条件,如:
if myVar == 1 { // Just for presenting
runMyAlert()
}
当在后台myVar == 1用户时,无论他在哪里,在哪个屏幕上,他都会在屏幕上得到警报。
如何在不重复代码的情况下做到这一点?我试图在 AppDelegate 中将其设置为:
func alert(title : String,message : String,buttonTitle : String,window: UIWindow){
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: buttonTitle, style: UIAlertActionStyle.Default, handler: nil))
window.rootViewController?.presentViewController(alert, animated: true, completion: nil)
}
并将其称为:
if myVar == 1 {
AppDelegate().alert()
}
【问题讨论】:
-
您可以编写一些代码来获取当前的顶视图控制器,然后您可以从任何地方呈现它....
rootViewController与您当前的顶视图控制器不同
标签: ios swift alert appdelegate