如何在不重复代码的情况下在所有控制器中显示警报?
How to show alert in all controllers without repeating the code?
我想编写一个函数 alert()
并 运行 它。但是我想在不重复代码的情况下在任何控制器中显示此警报。
例如:我有 Presence.swift class 这里我有一些条件,如:
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()
}
在UIViewController上创建一个category,在那里写一个方法,调用哪个app会显示一个alert box。
现在创建一个新的BaseViewController,它将继承自UIViewController。在此 BaseViewController 中导入您的类别;
从现在开始,每当您创建一个新的 viewController,select 您的基础 class 类型为 BaseViewController 而不是 UIViewController。
通过这样做,您可以从任何地方调用该警报显示方法。
在 Swift
上进行开发,您应该了解可以轻松解决您的问题的协议。您可以创建一个新协议 MyAlert
并为 UIViewController
class 设置默认实现。然后只需在视图控制器中继承您的 MyAlert
协议,在您需要的地方调用该函数!
protocol MyAlert {
func runMyAlert()
}
extension MyAlert where Self: UIViewController {
func runMyAlert() {
let alert = UIAlertController(title: title, message: "message", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "buttonTitle", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
}
所以你可以这样实现和调用它:
class MyViewController: UIViewController, MyAlert {
override func viewDidLoad() {
runMyAlert() // for test
}
}
更新:
您的案例代码:
protocol MyAlert {
func runMyAlert()
}
extension MyAlert where Self: UIViewController {
func runMyAlert() {
let alert = UIAlertController(title: title, message: "message", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "buttonTitle", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
}
class OpenChatsTableViewController: UITableViewController, MyAlert, OneRosterDelegate, BuddyRequestProtocol {
override func viewDidLoad() {
runMyAlert()
}
}
我想编写一个函数 alert()
并 运行 它。但是我想在不重复代码的情况下在任何控制器中显示此警报。
例如:我有 Presence.swift class 这里我有一些条件,如:
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()
}
在UIViewController上创建一个category,在那里写一个方法,调用哪个app会显示一个alert box。
现在创建一个新的BaseViewController,它将继承自UIViewController。在此 BaseViewController 中导入您的类别;
从现在开始,每当您创建一个新的 viewController,select 您的基础 class 类型为 BaseViewController 而不是 UIViewController。
通过这样做,您可以从任何地方调用该警报显示方法。
在 Swift
上进行开发,您应该了解可以轻松解决您的问题的协议。您可以创建一个新协议 MyAlert
并为 UIViewController
class 设置默认实现。然后只需在视图控制器中继承您的 MyAlert
协议,在您需要的地方调用该函数!
protocol MyAlert {
func runMyAlert()
}
extension MyAlert where Self: UIViewController {
func runMyAlert() {
let alert = UIAlertController(title: title, message: "message", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "buttonTitle", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
}
所以你可以这样实现和调用它:
class MyViewController: UIViewController, MyAlert {
override func viewDidLoad() {
runMyAlert() // for test
}
}
更新:
您的案例代码:
protocol MyAlert {
func runMyAlert()
}
extension MyAlert where Self: UIViewController {
func runMyAlert() {
let alert = UIAlertController(title: title, message: "message", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "buttonTitle", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
}
class OpenChatsTableViewController: UITableViewController, MyAlert, OneRosterDelegate, BuddyRequestProtocol {
override func viewDidLoad() {
runMyAlert()
}
}