Skip to content

Instantly share code, notes, and snippets.

@mono0926
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mono0926/5af3fcdc872eb506002f to your computer and use it in GitHub Desktop.
Save mono0926/5af3fcdc872eb506002f to your computer and use it in GitHub Desktop.
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment