Skip to content

Instantly share code, notes, and snippets.

@davideme
Created January 30, 2018 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save davideme/cb19a6e1256d4b0f14ae91653d2ebfbb to your computer and use it in GitHub Desktop.
Save davideme/cb19a6e1256d4b0f14ae91653d2ebfbb to your computer and use it in GitHub Desktop.
LifecycleObserver with Notification
//
// ViewController.swift
// LifeCycleDelegate
//
// Created by Davide Mendolia on 30/01/2018.
// Copyright © 2018 Karumi. All rights reserved.
//
import UIKit
@objc protocol ViewControllerLifecycleObserver2 {
@objc optional func viewDidLoad()
@objc optional func viewWillAppear()
@objc optional func viewDidAppear()
@objc optional func viewWillDisappear()
@objc optional func viewDidDisappear()
}
class BaseViewControllerWithLifecycleNotification: UIViewController {
var lifecycleDeletage: ViewControllerLifecycleObserver?
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.post(name: NSNotification.Name.ViewDidLoad, object: self)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.post(name: NSNotification.Name.ViewWillAppear, object: self)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
NotificationCenter.default.post(name: NSNotification.Name.ViewDidAppear, object: self)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.post(name: NSNotification.Name.ViewWillDisappear, object: self)
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
NotificationCenter.default.post(name: NSNotification.Name.ViewDidDisappear, object: self)
}
}
class Presenter2: ViewControllerLifecycleObserver2 {
func viewDidLoad() {
print("Trigger on view did load")
}
}
extension NSNotification.Name {
static let ViewDidLoad = Notification.Name("ViewDidLoad")
static let ViewWillAppear = Notification.Name("viewWillAppear")
static let ViewDidAppear = Notification.Name("viewDidAppear")
static let ViewWillDisappear = Notification.Name("viewWillDisappear")
static let ViewDidDisappear = Notification.Name("viewDidDisappear")
}
func provideViewController() {
let vc = BaseViewControllerWithLifecycleNotification()
let presenter = Presenter2()
presenter.bind(to: vc)
}
extension ViewControllerLifecycleObserver2 {
func bind(to vc: BaseViewControllerWithLifecycleNotification) {
NotificationCenter.default.addObserver(
self, selector:
#selector(ViewControllerLifecycleObserver.viewDidLoad),
name: NSNotification.Name.ViewDidLoad,
object: vc
)
NotificationCenter.default.addObserver(
self, selector:
#selector(ViewControllerLifecycleObserver.viewWillAppear),
name: NSNotification.Name.ViewWillAppear,
object: vc
)
NotificationCenter.default.addObserver(
self, selector:
#selector(ViewControllerLifecycleObserver.viewDidAppear),
name: NSNotification.Name.ViewDidAppear,
object: vc
)
NotificationCenter.default.addObserver(
self, selector:
#selector(ViewControllerLifecycleObserver.viewWillDisappear),
name: NSNotification.Name.ViewWillDisappear,
object: vc
)
NotificationCenter.default.addObserver(
self, selector:
#selector(ViewControllerLifecycleObserver.viewDidDisappear),
name: NSNotification.Name.ViewDidDisappear,
object: vc
)
}
}
@linhnobi
Copy link

Where can I see an example you apply?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment