Last active
August 12, 2016 11:10
-
-
Save ppeszko/8bbb64c9c5eef0902023c4814aa7a1d3 to your computer and use it in GitHub Desktop.
Sad View Controller example for https://medium.com/@PatrykPeszko/notification-pattern-a862ceff4226
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
| import UIKit | |
| struct Payment { } | |
| enum PaymentResponse { | |
| case Accepted(Payment) | |
| case Rejected(Payment) | |
| } | |
| // It's just an example for notifications. The rest can be safely ignored :) | |
| class SadPaymentViewController: UIViewController { | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| let notificationCenter = NSNotificationCenter.defaultCenter() | |
| // MARK: ლ( `Д’ ლ) | |
| // This tells Notfication Center that we are interested in updates. | |
| notificationCenter.addObserver(self, | |
| selector: #selector(SadPaymentViewController.paymentNotificationReceived(_:)), | |
| name: "PaymentViewControllerNotificationRejected", | |
| object: nil) | |
| notificationCenter.addObserver(self, | |
| selector: #selector(SadPaymentViewController.paymentNotificationReceived(_:)), | |
| name: "PaymentViewControllerNotificationAccepted", | |
| object: nil) | |
| } | |
| func paymentServiceDidReturn(paymentResponse: PaymentResponse) { | |
| let notificationCenter = NSNotificationCenter.defaultCenter() | |
| switch paymentResponse { | |
| case .Accepted(_): | |
| // MARK: •́︵•̀ | |
| // And this is how we post notifications | |
| notificationCenter.postNotificationName("PaymentViewControllerNotificationRejected", | |
| object: nil, | |
| userInfo: nil) | |
| case .Rejected(_): | |
| notificationCenter.postNotificationName("PaymentViewControllerNotificationAccepted", | |
| object: nil, | |
| userInfo: nil) | |
| } | |
| } | |
| func paymentNotificationReceived(notification: NSNotification) { | |
| //FIXIT: Write this code before release, okay? Bye, I'm off for vacation. ᕕ( ᐛ )ᕗ | |
| } | |
| deinit { | |
| NSNotificationCenter.defaultCenter().removeObserver(self) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment