Create a gist now

Instantly share code, notes, and snippets.

// Swift 3
import UIKit
class TestView: UIView {
// MARK: Observers
override func didMoveToWindow() {
if self.window != nil {
NotificationCenter.default.addObserver(self, selector: #selector(test(_:)), name: NSNotification.Name(rawValue: "TestNotification"), object: nil)
}
}
override func willMove(toWindow newWindow: UIWindow?) {
if newWindow == nil {
NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: "TestNotification"), object: nil)
}
}
// MARK: Actions
func test(_ notification : Notification) {
print("Test")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment