ios broadcasting event using NotificationCenter
1. Create a custom event call AButtonIsTapped
extension Notification.Name {
static let AButtonIsTapped = Notification.Name("AButtonIsTapped")
}
2. Create some observer objects.
private var buttonTapObserver: NSObjectProtocol? private var buttonTapObserver2: NSObjectProtocol? private var buttonTapObserver3: NSObjectProtocol?
3. Initialize the observers and start observe.
// listens to the AButtonIsTapped event and it can come from any object
self.buttonTapObserver = NotificationCenter.default.addObserver(
forName: .AButtonIsTapped,
object: nil,
queue: OperationQueue.main,
using: { notification in
print("observer111111: A button is tapped")
}
)
// listens to the AButtonIsTapped event and it has to come from the tapButton object
self.buttonTapObserver2 = NotificationCenter.default.addObserver(
forName: .AButtonIsTapped,
object: self.tapButton,
queue: OperationQueue.main,
using: { notification in
print("observer222222: A button is tapped")
}
)
// listens to the AButtonIsTapped event and it has to come from self, this view controller
self.buttonTapObserver3 = NotificationCenter.default.addObserver(
forName: .AButtonIsTapped,
object: self,
queue: OperationQueue.main,
using: { notification in
print("observer333333: A button is tapped")
}
)
4. Send event to the NotificationCenter.
// post the AButtonIsTapped event with the tapButton object, only buttonTapObserver and buttonTapObserver1 will observe this, buttonTapObserver3 will not because it only observes object coming from this view controller NotificationCenter.default.post(name: .AButtonIsTapped, object: sender)
5. Removing observers. If your app targets iOS 9.0 and later or macOS 10.11 and later, you don’t need to unregister an observer in its dealloc method. // https://developer.apple.com/documentation/foundation/notificationcenter/1413994-removeobserver
if let observer1 = buttonTapObserver1 {
print("Removing buttonTapObserver1")
NotificationCenter.default.removeObserver(observer1)
}
if let observer2 = buttonTapObserver2 {
print("Removing buttonTapObserver2")
NotificationCenter.default.removeObserver(observer2)
}
if let observer3 = buttonTapObserver3 {
print("Removing buttonTapObserver3")
NotificationCenter.default.removeObserver(observer3)
}
6. All of the above in one ViewController. The event can be posted from another controller/object/view and be observed in this or anywhere in the app that observes that particular event.
import UIKit
extension Notification.Name {
static let AButtonIsTapped = Notification.Name("AButtonIsTapped")
}
class ViewController: UIViewController {
private var buttonTapObserver: NSObjectProtocol?
private var buttonTapObserver2: NSObjectProtocol?
private var buttonTapObserver3: NSObjectProtocol?
@IBOutlet weak var tapButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// listens to the AButtonIsTapped event and it can come from any object
self.buttonTapObserver = NotificationCenter.default.addObserver(
forName: .AButtonIsTapped,
object: nil,
queue: OperationQueue.main,
using: { notification in
print("observer111111: A button is tapped")
}
)
// listens to the AButtonIsTapped event and it has to come from the tapButton object
self.buttonTapObserver2 = NotificationCenter.default.addObserver(
forName: .AButtonIsTapped,
object: self.tapButton,
queue: OperationQueue.main,
using: { notification in
print("observer222222: A button is tapped")
}
)
// listens to the AButtonIsTapped event and it has to come from self, this view controller
self.buttonTapObserver3 = NotificationCenter.default.addObserver(
forName: .AButtonIsTapped,
object: self,
queue: OperationQueue.main,
using: { notification in
print("observer333333: A button is tapped")
}
)
}
@IBAction func buttonTapped(_ sender: UIButton) {
// post the AButtonIsTapped event with the tapButton object, only buttonTapObserver and buttonTapObserver1 will observe this, buttonTapObserver3 will not because it only observes object coming from this view controller
NotificationCenter.default.post(name: .AButtonIsTapped, object: sender)
}
}
Search within Codexpedia
Custom Search
Search the entire web
Custom Search
Related Posts