Delegates in swift?

前端 未结 12 1325
天命终不由人
天命终不由人 2020-11-22 03:27

How does one go about making a delegate, i.e. NSUserNotificationCenterDelegate in swift?

12条回答
  •  甜味超标
    2020-11-22 03:59

    It is not that different from obj-c. First, you have to specify the protocol in your class declaration, like following:

    class MyClass: NSUserNotificationCenterDelegate
    

    The implementation will look like following:

    // 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
    }
    

    Of course, you have to set the delegate. For example:

    NSUserNotificationCenter.defaultUserNotificationCenter().delegate = self;
    

提交回复
热议问题