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/d81aab4f3a48e9d53ecf962186060aeb to your computer and use it in GitHub Desktop.
Save josh-marasigan/d81aab4f3a48e9d53ecf962186060aeb 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
class ViewController: UIViewController {
.
.
.
// MARK: - Button Configuration
private func configChangeBackgroundButtonUI() {
.
.
self.changeBackgroundButton.addTarget(
self,
action: #selector(changeBackgroundButtonAction),
for: .touchUpInside)
.
.
}
.
.
.
// MARK: - Button Action
@objc func changeBackgroundButtonAction(sender: UIButton!) {
.
.
.
/*
Then we set the notification name.
This will allow the observers who care about
this event that the event is occuring.
(Observer keys are unique identifiers used
to distinguish events from one another)
We also check if the current background
is colored teal, and if so,
send an event to change it to pink.
If not, then send an event to change it to teal
*/
let notificationIdentifier = (isBackgroundTeal) ?
Notification.Name(rawValue: ObserverKeys.pink) :
Notification.Name(rawValue: ObserverKeys.teal)
/*
Post the notification informing any observers
listening to this event (indicated by its name)
that an event had occured.
If an observer registered to listen for
an event with name 'notificationIdentifier',
then they will receive this notification
*/
NotificationCenter.default.post
(
name: notificationIdentifier,
object: nil
)
.
.
}
.
.
.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment