ViewController 代码规范
@property(nonatomic,strong) UIButton * Button;
#pragma mark -life cycle
viewDidLoad
viewWillAppear
#pragma mark - UITableViewDelegate
methods
#pragma mark - CustomDelegate
methods
#pragma mark - event response
-(void)didTappedConfirmButton:(UIButton *)confirmButton
#pragma mark - private methods
methods
#pragma mark - getters and setters
-(UIButton *)confirmButton
-(UITableView *)tableView
1.所有的属性都使用getter和setter
#pragma mark - life cycle
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.firstTableView];
[self.view addSubview:self.secondTableView];
[self.view addSubview:self.firstFilterLabel];
[self.view addSubview:self.secondFilterLabel];
[self.view addSubview:self.cleanButton];
[self.view addSubview:self.originImageView];
[self.view addSubview:self.processedImageView];
[self.view addSubview:self.activityIndicator];
[self.view addSubview:self.takeImageButton];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
CGFloat width = (self.view.width - 30) / 2.0f;
self.originImageView.size = CGSizeMake(width, width);
[self.originImageView topInContainer:70 shouldResize:NO];
[self.originImageView leftInContainer:10 shouldResize:NO];
self.processedImageView.size = CGSizeMake(width, width);
[self.processedImageView right:10 FromView:self.originImageView];
[self.processedImageView topEqualToView:self.originImageView];
CGFloat labelWidth = self.view.width - 100;
self.firstFilterLabel.size = CGSizeMake(labelWidth, 20);
[self.firstFilterLabel leftInContainer:10 shouldResize:NO];
[self.firstFilterLabel top:10 FromView:self.originImageView];
... ...
}
尽量不要出现以下的情况:
- (void)viewDidLoad
{
[super viewDidLoad];
self.textLabel = [[UILabel alloc] init];
self.textLabel.textColor = [UIColor blackColor];
self.textLabel ... ...
self.textLabel ... ...
self.textLabel ... ...
[self.view addSubview:self.textLabel];
}
2.getter和setter全部都放在最后
写代码的时候按照顺序来分配代码块的位置,先是life cycle
,然后是Delegate
方法实现,然后是event response
,然后才是getters and setters
。
3.每一个delegate都把对应的protocol名字带上,delegate方法不要到处乱写,写到一块区域里面去
比如UITableViewDelegate
的方法集就老老实实写上#pragma mark - UITableViewDelegate
。这样有个好处就是,当其他人阅读一个他并不熟悉的Delegate实现方法时,他只要按住command然后去点这个protocol名字,Xcode就能够立刻跳转到对应这个Delegate的protocol定义的那部分代码去,就省得他到处找了。
4.event response专门开一个代码区域
所有button、gestureRecognizer的响应事件都放在这个区域里面,不要到处乱放。
5.关于private methods,正常情况下ViewController里面不应该写
View布局
1.在针对 View 层这边的要求时,建议不要用 StoryBoard。
实现简单的东西,用Code一样简单,实现复杂的东西,Code比StoryBoard 更简单。所以我更加提倡用 code 去画 view 而不是 storyboard。(引自唐巧)