本文是在控制器的基礎(chǔ)上添加了一個(gè) Category
,只需導(dǎo)入 Category
并引用頭文件娶桦,不需要在項(xiàng)目里引用其他代碼浦楣,就可以檢測(cè)控制器是否被銷毀。具體代碼如下:
UIViewController+Example.h
//
// UIViewController+Example.h
//
// Created by TL on 2017/4/6.
// Copyright ? 2017年 TL. All rights reserved.
//
#import "UIViewController.h"
@interface RootViewController (Example)
@property (copy, nonatomic) NSString *method;
@end
UIViewController+Example.m
//
// UIViewController+Example.m
//
// Created by TL on 2017/4/6.
// Copyright ? 2017年 TL. All rights reserved.
//
#import "UIViewController+Example.h"
@implementation UIViewController (Example)
#pragma mark - swizzle
+ (void)load
{
Method method1 = class_getInstanceMethod([self class], NSSelectorFromString(@"dealloc"));
Method method2 = class_getInstanceMethod([self class], @selector(deallocSwizzle));
method_exchangeImplementations(method1, method2);
}
- (void)deallocSwizzle
{
NSLog(@"%@被銷毀了", self);
}
static char MethodKey;
- (void)setMethod:(NSString *)method
{
objc_setAssociatedObject(self, &MethodKey, method, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (NSString *)method
{
return objc_getAssociatedObject(self, &MethodKey);
}
@end
沒有一蹴而就的天才火欧,只有不斷點(diǎn)亮天賦的勤者