2015年3月18日 星期三

統一收notification再以delegate分派出去的設計方式

//在UIEvents統一接收notification
@objc protocol UIEvents {
    func onError(notification: NSNotification)
}

class UIEventHelper {

    class func registerFusionEvents(eventReceiver: AnyObject) {
        NSNotificationCenter.defaultCenter().addObserver(
            eventReceiver, selector: "onFusionError:",
            name: FusionService.fusionErrorNotification, object: nil)

    }
}

//在你要使用的class,加入註冊跟實作delegate
//viewWillAppear
self.registerNotifications()
//viewWillDisappear
self. unregisterNotifications()

    // MARK: - Notification
func registerNotifications() {
    UIEventHelper.registerFusionEvents(self)
}
    
func unregisterNotifications() {
    UIEventHelper.unregisterUIEvents(self)
}

func onError(notification: NSNotification) {
//TODO:實作你的code
}


沒有留言:

張貼留言