开发Swift
你应该知道可以轻松解决问题的协议.您可以创建新协议MyAlert
并为UIViewController
类创建默认实现.然后只需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 } }
UPD:
您案件的代码:
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() } }