Last active
June 21, 2019 10:58
-
-
Save olxios/335668667643a59c0b6f5986bb6bf46f to your computer and use it in GitHub Desktop.
Using NotificationCenter with UIView subclass: http://swiftiostutorials.com/quick-tip-adding-removing-nsnotificationcenter-observers-uiview-subclass/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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