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- model
3 // 定义model
4 #pragma mark- view
5 // 定义view
6 #pragma mark- api
7 // 定义api
8  
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的

  1. @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

     

  2. 欢迎拍砖指正。



posted @ 2015-01-20 15:13  water.wjf  阅读(350)  评论(0编辑  收藏  举报