UIViewController+Example.m 872 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. //
  2. // UIViewController+Example.m
  3. // MJRefreshExample
  4. //
  5. // Created by MJ Lee on 15/3/12.
  6. // Copyright (c) 2015年 小码哥. All rights reserved.
  7. //
  8. #import "UIViewController+Example.h"
  9. #import <objc/runtime.h>
  10. @implementation UIViewController (Example)
  11. #pragma mark - swizzle
  12. + (void)load
  13. {
  14. Method method1 = class_getInstanceMethod([self class], NSSelectorFromString(@"dealloc"));
  15. Method method2 = class_getInstanceMethod([self class], @selector(deallocSwizzle));
  16. method_exchangeImplementations(method1, method2);
  17. }
  18. - (void)deallocSwizzle
  19. {
  20. NSLog(@"%@被销毁了", self);
  21. [self deallocSwizzle];
  22. }
  23. static char MethodKey;
  24. - (void)setMethod:(NSString *)method
  25. {
  26. objc_setAssociatedObject(self, &MethodKey, method, OBJC_ASSOCIATION_COPY_NONATOMIC);
  27. }
  28. - (NSString *)method
  29. {
  30. return objc_getAssociatedObject(self, &MethodKey);
  31. }
  32. @end