Skip to content

Instantly share code, notes, and snippets.

@josh-marasigan
Last active April 30, 2018 03:13
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 josh-marasigan/b55c0d407929c68424927b7505679d5e to your computer and use it in GitHub Desktop.
Save josh-marasigan/b55c0d407929c68424927b7505679d5e to your computer and use it in GitHub Desktop.
// `.` indicates code left out.
// Please refer to the sample app for reference
// https://github.com/josh-marasigan/ObserverDesignExample
// Somewhere in the TitleAndTextView.swift file
class TitleAndTextView: UIView {
.
.
.
// MARK: - Class Properties. Fun fact: emojis are just stirng literals
private let titleLabel = UILabel()
private let emoji = UILabel()
private var subTitleLabel = UILabel()
.
.
.
init() {
super.init(frame: .zero)
self.configObservers()
.
}
.
.
.
// Initialize Observers
private func configObservers() {
let tealNotificationName = Notification.Name(rawValue: ObserverKeys.teal)
NotificationCenter.default.addObserver(
self,
selector: #selector(changeSubTitleToTeal(notification:)),
name: tealNotificationName,
object: nil
)
NotificationCenter.default.addObserver(
self,
selector: #selector(changeEmojiAccordingToTeal(notification:)),
name: tealNotificationName,
object: nil
)
let pinkNotificationName = Notification.Name(rawValue: ObserverKeys.pink)
NotificationCenter.default.addObserver(
self,
selector: #selector(changeSubTitleToPink(notification:)),
name: pinkNotificationName,
object: nil
)
NotificationCenter.default.addObserver(
self,
selector: #selector(changeEmojiAccordingToPink(notification:)),
name: pinkNotificationName,
object: nil
)
}
.
.
.
// MARK: - Actions
@objc func changeSubTitleToTeal(notification: Notification) {
self.subTitleLabel.text = "Teal"
}
@objc func changeEmojiAccordingToTeal(notification: Notification) {
self.emoji.text = "🗽"
}
@objc func changeSubTitleToPink(notification: Notification) {
self.subTitleLabel.text = "Pink"
}
@objc func changeEmojiAccordingToPink(notification: Notification) {
self.emoji.text = "💕"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment