代码规范
代码具有一致性,一般都比较便于阅读和管理,每个人不一样,但是总之要整洁比较好,我的习惯是
- 视图使用自动布局
- 所有控件都有一个弱指针指向(参见我的Property强弱讨论文章)
- 初始化数据/初始化控件/布局/样式调整/动作分开管理
以下是代码示范
@interface BPLoginMainViewController ()
@property (nonatomic, weak) id name;//指向控件的弱指针
@end
@implementation BPLoginMainViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//初始化数据
[self dataInital];
//
[self instituteSubviews];//初始化控件
[self constrainSubviewsLayout];//对控件进行约束
[self modifySubviewsPattern];//对控件进行样式调整
//添加动作/通知/手势等
[self addActionAndNotification];
}
- (void)dataInital {
}
#pragma mark - View Layout Pattern
- (void)instituteSubviews {
//Init Subviews
<#type#> *<#name#> = [[<#type#> alloc]init];
//Weak Point
_<#name#> = <#name#>;
//Add Subviews
[<#view#> addSubview:_<#name#>];
}
- (void)constrainSubviewsLayout {
//Set autorizingMask
[_<#name#> setTranslatesAutoresizingMaskIntoConstraints:NO];
//AutoLayout VF
NSDictionary *views = NSDictionaryOfVariableBindings(_<#name#>);
NSDictionary *metrics = @{};
NSMutableArray *constraints = [NSMutableArray array];
[constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"" options:0 metrics:metrics views:views]];
[<#view#> addConstraints:constraints];
//不通过数组直接添加约束数组
// [<#view#> addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"" options:0 metrics:metrics views:views]];
//Single添加单个约束
// [<#view#> addConstraint:[NSLayoutConstraint constraintWithItem:_<#name#> attribute:<#attribute#> relatedBy:<#relation#> toItem:_<#view#> attribute:<#attribute#> multiplier:1.0 constant:0]];
}
- (void)modifySubviewsPattern {
//Modify Pattern
}
#pragma mark - Action 添加所有动作和动作响应
- (void)addActionAndNotification {
//Action
[<#name#> addTarget:<#target#> action:<#action#> forControlEvents:<#UIControlEventTouchUpInside#>]
//Notification
[[NSNotificationCenter defaultCenter] addObserver:<#target#> selector:<#action#> name:<#NSNotification#> object:<#object#>];
//Gesture
UITapGestureRecognizer *<#gesture#> = [[UITapGestureRecognizer alloc]initWithTarget:<#target#> action:<#action#>];
}
@end