标签:释放 runtime 控制 objects track app 创建 efi receive
官方的介绍例如以下
The UIViewController class provides the fundamental view-management model for all iOS apps. You rarely instantiate UIViewController objects directly. Instead, you instantiate subclasses of the UIViewController class based on the specific task each subclass performs.
简单的说就是一个视图控制器类
ios6以后取消了viewWillUnload方法,apple官方文档说不是必需了(我勒个去了),大体就是view和显示分开了,当内存紧张的时候系统会帮你回收显示的贴图.这个贴图呢仅仅是先标记为已经清除,假设你没用到这块内存,下次须要显示这个view的时候,还能够立刻复用来.新的周期图大致例如以下.
一千个人眼里有一千个哈姆雷特, 假设不统一下, 每一个程序猿依照自己的习惯的代码风格去写, 会给维护造成了困扰,所以我们给UIViewController的申明周期中加上一些方法来规范下
loadView 中加上三个方法
viewDidLoad
dealloc
didReceiveMemoryWarning
额外
定义了执行时要做的事后,我们再来统一下文件内怎样写
首先是头文件
@interface UIViewController (base)
#pragma mark- model
// 定义model
#pragma mark- view
// 定义view
#pragma mark- api
// 定义api
@end
实现文件
#pragma mark - api
// 对外的接口
#pragma mark - rewrite
// 额外的重写的父类的方法
#pragma mark - private
#pragma mark - 响应 model 的地方
#pragma mark 1 notification
#pragma mark 2 KVO
#pragma mark - 响应 view 的地方
#pragma mark 1 target-action
#pragma mark 2 delegate dataSource protocol
#pragma mark -
至此呢,我们的UIViewController就有了一个统一的风格,在团队的扩建中,再统一下命名,代码看起来就舒服多了
实现能够用runtime,也能够用基类, 个人推荐是用基类,可是以下的代码是runtime的
@implementation UIViewController (base)
+ (void)load
{
XY_swizzleInstanceMethod([UIViewController class], @selector(loadView), @selector(xy__loadView));
XY_swizzleInstanceMethod([UIViewController class], @selector(viewDidLoad), @selector(xy__viewDidLoad));
XY_swizzleInstanceMethod([UIViewController class], NSSelectorFromString(@"dealloc"), @selector(xy__dealloc));
XY_swizzleInstanceMethod([UIViewController class], @selector(didReceiveMemoryWarning), @selector(xy__didReceiveMemoryWarning));
}
- (void)xy__loadView
{
[self xy__loadView];
if ([self respondsToSelector:@selector(createFields)])
[self performSelector:@selector(createFields)];
if ([self respondsToSelector:@selector(createViews)])
[self performSelector:@selector(createViews)];
if ([self respondsToSelector:@selector(enterBackground)])
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(enterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
}
if ([self respondsToSelector:@selector(enterForeground)])
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(enterForeground) name:UIApplicationWillEnterForegroundNotification object:nil];
}
if ([self respondsToSelector:@selector(createEvents)])
[self performSelector:@selector(createEvents)];
}
- (void)xy__dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
if ([self respondsToSelector:@selector(destroyEvents)])
[self performSelector:@selector(destroyEvents)];
if ([self respondsToSelector:@selector(destroyViews)])
[self performSelector:@selector(destroyViews)];
if ([self respondsToSelector:@selector(destroyFields)])
[self performSelector:@selector(destroyFields)];
[self xy__dealloc];
}
- (void)xy__viewDidLoad
{
if ([self respondsToSelector:@selector(loadData)])
[self performSelector:@selector(loadData)];
[self xy__viewDidLoad];
}
- (void)xy__didReceiveMemoryWarning
{
if ([self isViewLoaded] && [self.view window] == nil)
{
if ([self respondsToSelector:@selector(cleanData)])
[self performSelector:@selector(cleanData)];
}
[self xy__didReceiveMemoryWarning];
}
@end
标签:释放 runtime 控制 objects track app 创建 efi receive
原文地址:http://www.cnblogs.com/wzjhoutai/p/6939696.html