mono0926
2/23/2015 - 4:11 AM

Register and remove notification

Register and remove notification

// extension
extension UIViewController {
    func observeDidBecomeActive(action: Selector) -> () -> () {
        return observeNotification(action, name: UIApplicationDidBecomeActiveNotification)
    }
    private func observeNotification(action: Selector, name: String) -> () -> () {
        NSNotificationCenter.defaultCenter().addObserver(self, selector: action, name: name, object: nil)
        return {
            return NSNotificationCenter.defaultCenter().removeObserver(self, name: name, object: nil)
        }
    }
}
    
// 登録
let removeObserverClosure = observeDidBecomeActive("didBecomeActive")
    
// どこかで解除
removeObserverClosure()