代码组织

1)整个implementation file应该由#pragma mark -来分类方法;

2)pragma与方法名不需要空行;

以ViewController为例,按照以下顺序书写代码:

  • 如果有init系列方法,以#pragma mark - Initialization Methods分类;
  • 生命周期方法,以#pragma mark - Life Cycle分类;
  • 如果需要重写父类的方法,以#pragma mark - Override分类;
  • 如果有系统的delegate方法,以#pragma mark - 系统代理名字分类;
  • 如果有自定义的delegate方法,以#pragma mark - 自定义代理名字分类;
  • 如果有系统的Notification方法,以#pragma mark - System Notifaction分类;
  • 如果有自定义的Notication方法,以#pragma mark - Custom Notification分类;
  • 如果有事件响应,比如点击事件,以#pragma mark - Event Response分类;
  • 如果有public方法,以#pragma mark - Public Methods分类;
  • 私有方法,以#pragma mark - Private Methods分类;
  • 最后是属性的get和set方法,以#pragma mark - Getters and Setters分类。
#pragma mark - Initialization Methods
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

- (instancetype)initWithHouseArray:(NSArray *)houseArray {
    self = [super init];
    if (self) {
        _houseArray = houseArray;
    }
    return self;
}

#pragma mark - Life Cycle
- (void)viewDidLoad {
    [super viewDidLoad]; 
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillShow:) 
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(sipRegistrationStatusChange:)
                                                 name:HoriSipRegistrationStatusNotification 
                                               object:nil];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIKeyboardWillShowNotification
                                                  object:nil];
}

#pragma mark - Override
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
}

#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

#pragma mark - HoriBottomViewDelegate
- (void)bootomView:(HoriBottomView *)bottomView didClickBottomWithButton:(UIButton *)button {
}

#pragma mark - System Notification
- (void)keyboardWillShow:(NSNotification *)notification {
}

#pragma mark - Custom Notification
- (void)sipRegistrationStatusChange:(NSNotification *)notification {


#pragma mark - Event Response
- (void)didClickLoginButton:(UIButton *)button {
}

#pragma mark - Public Methods
- (void)publcMethodName {
}

#pragma mark - Private Methods
- (void)setupNavigationItem {
}

#pragma mark - Getters and Setters
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
    }
    return _tableView;
}

3)在viewDidLoad方法里添加subView,然后另开一个方法用来给view进行布局:

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.view addSubView:self.topView];
    [self.view addSubView:self.middleView];
    [self.view addSubView:self.bottomView];

    [self layoutPageSubViews];
}

- (void)layoutPageSubViews {
    [self.topView addConstraints:aaaConstraints];
    [self.middleView addConstraints:bbbConstraints];
    [self.bottomView addConstraints:cccConstraints];
}

4)尽量使用代码写View,不要用storyboard,少用nib!

results matching ""

    No results matching ""