如何做一个委托,即NSUserNotificationCenterDelegate在swift?
它与obj-c没有什么不同。
首先,必须在类声明中指定协议,如下所示:
首先,必须在类声明中指定协议,如下所示:
class MyClass: NSUserNotificationCenterDelegate
实现将如下所示:
// NSUserNotificationCenterDelegate implementation func userNotificationCenter(center: NSUserNotificationCenter,didDeliverNotification notification: NSUserNotification) { //implementation } func userNotificationCenter(center: NSUserNotificationCenter,didActivateNotification notification: NSUserNotification) { //implementation } func userNotificationCenter(center: NSUserNotificationCenter,shouldPresentNotification notification: NSUserNotification) -> Bool { //implementation return true }
当然,你必须设置委托。例如:
NSUserNotificationCenter.defaultUserNotificationCenter().delegate = self;