第一种
UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 30)];
subView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:subView];
第二种
优点:
1.使代码逻辑结构层次划分得更加明显
2.对于一个无需复用的小段逻辑也免去了重量级的调用函数
3.当你想要复制粘贴这段代码时,你仅需要更改一处变量名即可
UIView *subView = ({
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 200, 100, 30)];
view.backgroundColor = [UIColor redColor];
[self.view addSubview:view];
view;
});
subView.backgroundColor = [UIColor yellowColor];
网友评论