首页 > 代码库 > UIViewController生命周期控制-开发规范
UIViewController生命周期控制-开发规范
从网上各位iOS们收集并总结:
各方法使用:
- init 中初始化一些UI组件,比如UIButton,UILabel等
- loadView 中
- createFields 接受参数,初始化变量
- createViews 创建视图
- createEvents 绑定事件,如按钮的点击,NotificationCenter,kvo等
- viewDidLoad
- loadData 加载数据,调用一些api
- dealloc(现在dealloc中做的事我放在了viewDidDisapper中)
- destroyEvents 取消事件的绑定
- destroyViews 释放,销毁视图
- destroyFields 释放,销毁引用的变量
- didReceiveMemoryWarning
- cleanData 释放一些可以释放的数据
- 额外
- enterForeground 进入前台时调用
- enterBackground 进入后台时调用
规范文件内部组织结构:
统一UIViewController风格,首先是头文件:
1 @interfaceUIViewController(base)2 #pragma mark- model3 // 定义model4 #pragma mark- view5 // 定义view6 #pragma mark- api7 // 定义api8 9 @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 2delegate dataSource protocol//...#pragma mark -其他//...
统一命名:
实现可以用runtime,也可以用基类,个人推荐是用基类,但是下面的代码是runtime的
@implementationUIViewController(base)+(void)load { XY_swizzleInstanceMethod([UIViewControllerclass],@selector(loadView),@selector(xy__loadView)); XY_swizzleInstanceMethod([UIViewControllerclass],@selector(viewDidLoad),@selector(xy__viewDidLoad)); XY_swizzleInstanceMethod([UIViewControllerclass],NSSelectorFromString(@"dealloc"),@selector(xy__dealloc)); XY_swizzleInstanceMethod([UIViewControllerclass],@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
- 欢迎拍砖指正。
来自为知笔记(Wiz)
UIViewController生命周期控制-开发规范
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。