IBAction和IBOutlet
- IBAction:只有返回值声明为IBAction的方法,才能跟storyboard中的控件进行连线
@property (weak, nonatomic) IBOutlet UIButton *btn;
- IBOutlet:只有声明为IBOutlet的属性,才能跟storyboard中的控件进行连线
-(IBAction)delete1{//移除控件[self.myTitle removeFromSuperview];}
拖线的其他方式
一个对象能连接多个方法
一个对象能连接多个属性(没必要)
判断一个方法是否能连线,看控件是否继承UIControl
按住control键将控件拖至代码编辑框
拖线的常见错误
- 有多余的连线
NSUnknownKeyException', reason:
[<ViewController 0x7fdf7048b200> setValue:forUndefinedKey:]:
this class is not key value coding-compliant for the key
解决办法:删除多余的连线
- 找不到对应的方法
'NSInvalidArgumentException', reason:
'-[ViewController delete2]:
unrecognized selector sent to instance
>解决办法:添加对应的方法或者删除多余的连线
UIView介绍
- 控件:界面中的所有元素
- 控件的共有属性:尺寸、位置、背景色等等
- 控件的共有属性封装在其父类——UIView中
UIView常见属性
- superView:查看父控件
NSLog(@"%@",self.greenView.superview);
- subView:查看子控件
NSLog(@"%@",self.greenView.subviews);
- 查看View中控件的数量
NSLog(@"%lu",self.view.subviews.count);
- 控制器的父控件:UIWindow
-(void)viewDidAppear:(BOOL)animated{
NSLog(@"%@",self.view.superview);
}
UIView的常见方法
- addsubView :添加一个子控件
//实例化一个switch
UISwitch *sw1 = [[UISwitch alloc] init];
//在yellowView中添加uiBtn
[self.yellowView addSubview:sw1];
- removeFromSuperview:移除控件
[self.myTitle removeFromSuperview];
- viewWithTag:根据一个标识
注意:尽量少用Tag
UIViewc常见属性:尺寸和位置
- frame :控件矩形框在父控件的位置和尺寸
UIView *buleView = [[UIView alloc] init];
//CGRectMake(x, y, w, h)
buleView.frame = CGRectMake(100, 100, 100, 100);
- bounds:控件矩形框的位置和尺寸(以自己左上角为坐标原点,所以bounds的xy通常为0)
buleView.bounds = CGRectMake(0, 0, 300, 100)
- center:控件中点的位置(以父控件的左上角为坐标原点)
buleView.center =
CGPointMake(self.view.frame.size.width * 0.5,
self.view.frame.size.height *0.5);