我在单独的文件中有 2 个类:让我们称它们为 NotificationManagement 和 NotificationReceiver
在我的 NotificationReceiver 类中,我有以下代码:
class NotificationReceiver: UIViewController{
//...
func showNotificationHandler(alert: UIAlertAction!) {
var storyboard : UIStoryboard = UIStoryboard(name: Constants.storyboardName, bundle: nil)
var vc : Article = storyboard.instantiateViewControllerWithIdentifier(Constants.articleVCTitle) as! Article
self.presentViewController(vc, animated: false, completion: nil)
}
func dismissNotificationHandler(alert: UIAlertAction!) {
Constants.articleAddress = ""
}
func showAlert(title:String, message:String) {
let alert = UIAlertController(title: title,
message: message, preferredStyle: .Alert)
let okAction = UIAlertAction(title: Messages.showMsg, style: .Default , handler: showNotificationHandler)
let dismissAction = UIAlertAction(title: Messages.discardMsg, style: .Cancel, handler: dismissNotificationHandler)
alert.addAction(dismissAction)
alert.addAction(okAction)
self.presentViewController(alert, animated: false, completion: nil)
}
//...
}
现在我想把这个通知的处理放在 NotificationManagement 类而不是 NotificationReceiver 类中。但是,如果我确实把它放在那里,我需要在处理程序中使用类似的东西,以传递有关当前视图控制器的信息:
func showNotificationHandler(alert: UIAlertAction!, currentViewController: UIViewController) {
var storyboard : UIStoryboard = UIStoryboard(name: Constants.storyboardName, bundle: nil)
var vc : Article = storyboard.instantiateViewControllerWithIdentifier(Constants.articleVCTitle) as! Article
currentViewController.presentViewController(vc, animated: false, completion: nil)
}
但是后来我无法编译,并且错误确实具有误导性,例如编译器找不到样式 .Default (这是因为我向 showNotificationHandler 添加了参数)。我想像这样使用 NotificationReceiver 中的函数:
let notificationManagement = NotificationManagement()
notificationManagement.showAlert("title",message: "message")