Last active
April 30, 2018 03:13
-
-
Save josh-marasigan/b55c0d407929c68424927b7505679d5e to your computer and use it in GitHub Desktop.
This file contains 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
// `.` 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