在OC开发中,Storyboard中的全部操做均可以经过代码实现,程序员必定要熟练掌握代码布局界面的能力!布局
设置控件监听方法的示例代码以下:动画
[btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];spa
提示:code
1> addTarget方法定义在UIControl类中,这意味着能够给全部继承自UIControl类的对象添加监听方法orm
2> 监听方法的第一个参数就是对象自己对象
3> 监听方法的第二个参数是监听控件的事件blog
//1.使用类建立一个按钮对象
// UIButton *headbtn=[[UIButton alloc] initWithFrame:CGRectMake(100 ,100, 100, 100)];
//设置按钮对象为自定义型
UIButton *headbtn=[UIButton buttonWithType:UIButtonTypeCustom];
//2.设置对象的各项属性
//(1)位置等通用属性设置
headbtn.frame=CGRectMake(100, 100, 100, 100);
//(2)设置普通状态下按钮的属性
[headbtn setBackgroundImage:[UIImage imageNamed:@"i"] forState:UIControlStateNormal];
[headbtn setTitle:@"点我!" forState:UIControlStateNormal];
[headbtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
//(3)设置高亮状态下按钮的属性
[headbtn setBackgroundImage:[UIImage imageNamed:@"a"] forState:UIControlStateHighlighted];
[headbtn setTitle:@"还行吧~" forState:UIControlStateHighlighted];
[headbtn setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
//3.把对象添加到视图中展示出来
[self.view addSubview:headbtn];
//注意点!
self.headImageView=headbtn;