Protocols.swift 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. //
  2. // Protocoles.swift
  3. // ThemeDemo
  4. //
  5. // Created by 邓永豪 on 2017/8/23.
  6. // Copyright © 2017年 dengyonghao. All rights reserved.
  7. //
  8. import UIKit
  9. enum ThemeStyle: Int {
  10. case `default` = 0
  11. case black
  12. case o2
  13. case online
  14. }
  15. protocol ThemeProtocol {
  16. }
  17. extension UIView {
  18. func updateTheme() {
  19. print("update view theme")
  20. }
  21. }
  22. extension UIViewController {
  23. func updateTheme() {
  24. print("update view controller theme")
  25. }
  26. }
  27. extension ThemeProtocol where Self: UIView {
  28. func addThemeObserver() {
  29. print("addViewThemeObserver")
  30. NotificationCenter.default.addObserver(self, selector: #selector(updateTheme), name: NSNotification.Name(rawValue: kUpdateTheme), object: nil)
  31. }
  32. func removeThemeObserver() {
  33. print("removeViewThemeObserver")
  34. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: kUpdateTheme), object: nil)
  35. }
  36. }
  37. extension ThemeProtocol where Self: UIViewController {
  38. func addThemeObserver() {
  39. print("addViewControllerThemeObserver")
  40. NotificationCenter.default.addObserver(self, selector: #selector(updateTheme), name: NSNotification.Name(rawValue: kUpdateTheme), object: nil)
  41. }
  42. func removeThemeObserver() {
  43. print("removeViewControllerThemeObserver")
  44. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: kUpdateTheme), object: nil)
  45. }
  46. }