博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UIViewController生命周期控制
阅读量:6991 次
发布时间:2019-06-27

本文共 3701 字,大约阅读时间需要 12 分钟。

UIViewController生命周期控制

UIViewController介绍

官方的介绍例如以下

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以后的一般的生命周期

ios6以后取消了viewWillUnload方法,apple官方文档说不是必需了(我勒个去了),大体就是view和显示分开了,当内存紧张的时候系统会帮你回收显示的贴图.这个贴图呢仅仅是先标记为已经清除,假设你没用到这块内存,下次须要显示这个view的时候,还能够立刻复用来.新的周期图大致例如以下.

在生命周期中, 给要做的事情分类

一千个人眼里有一千个哈姆雷特, 假设不统一下, 每一个程序猿依照自己的习惯的代码风格去写, 会给维护造成了困扰,所以我们给UIViewController的申明周期中加上一些方法来规范下

  • loadView 中加上三个方法

    • createFields 接受參数,初始化变量
    • createViews 创建视图
    • createEvents 绑定事件,如button的点击,NotificationCenter,kvo等
  • viewDidLoad

    • loadData 载入数据,调用一些api
  • dealloc

    • destroyEvents 取消事件的绑定
    • destroyViews 释放,销毁视图
    • destroyFields 释放,销毁引用的变量
  • didReceiveMemoryWarning

    • cleanData 释放一些能够释放的数据
  • 额外

    • enterForeground 进入前台时调用
    • enterBackground 进入后台时调用

规范文件内部组织结构

定义了执行时要做的事后,我们再来统一下文件内怎样写

首先是头文件

@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
你可能感兴趣的文章
layoutSubviews总结
查看>>
目标检测的图像特征提取(一)HOG特点
查看>>
百度地图 Android SDK - 新的版本号(v3.2.0)正式上线
查看>>
TF-IDF模型的概率解释
查看>>
Android之桌面组件AppWidget
查看>>
使用ab进行页面的压力测试
查看>>
桌面轻量级数据库的选择:Access、SQLite、自己编写?
查看>>
Linux运维不可不知的性能监控和调试工具
查看>>
2015UESTC 暑假集训总结
查看>>
jQuery Post
查看>>
WebApiThrottle限流框架使用手册
查看>>
SQL Server里的自旋锁介绍
查看>>
成为高级程序员的 10 个步骤
查看>>
关于map与set的一点理解;
查看>>
可重复读隔离级别里的可能死锁
查看>>
[LeetCode]N-Queens 八皇后问题扩展(经典深层搜索)
查看>>
iOS开发-Launch Image和Launch Screen
查看>>
【Xamarin开发 Android 系列 3】循序渐进的学习顺序
查看>>
CSAPP 六个重要的实验 lab5
查看>>
迭代中删除元素
查看>>