我在单独的文件中有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")