155,纯代码创建标签和按钮,并关联点击事件
2015-12-30 21:59
423 查看
#import "ViewController.h"
@interface
ViewController ()
@property(weak,nonatomic)
IBOutlet UIButton *btn;
@property(weak,nonatomic)
IBOutlet UILabel *label;
@end
@implementation ViewController
-(void)viewDidLoad{
[super
viewDidLoad];
//使用alloc init实例化的按钮,就是custom类型的,按钮的类型一旦指定,就不能修改
//UIButton *btn =[[UIButton alloc]initWithFrame:CGRectMake(100, 100, 199, 100)];
UIButton *btn = [UIButton
buttonWithType:UIButtonTypeCustom];
self.btn = btn;
//添加点击事件
[btn addTarget:self
action:@selector(printPersonInfo:)
forControlEvents:UIControlEventTouchUpInside];
[btn setFrame:CGRectMake(100,
100, 100,
100)];
[btn setBackgroundImage:[UIImage
imageNamed:@"btn_01"]
forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage
imageNamed:@"btn_02"]
forState:UIControlStateHighlighted];
[btn setTitle:@"点我"
forState:UIControlStateNormal];
[btn setTitle:@"别点我"
forState:UIControlStateHighlighted];
[btn setTitleColor:[UIColor
redColor] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor
greenColor] forState:UIControlStateHighlighted];
btn.contentVerticalAlignment =
UIControlContentVerticalAlignmentBottom;
[self.view
addSubview:btn];
UILabel *label = [[UILabel
alloc]initWithFrame:CGRectMake(100,
300, 300,
50)];
self.label = label;
[self.view
addSubview:label];
}
-(IBAction)printPersonInfo:(UIButton *)button{
self.label.text
= @"我是JS,喜欢编程!";
}
@end
@interface
ViewController ()
@property(weak,nonatomic)
IBOutlet UIButton *btn;
@property(weak,nonatomic)
IBOutlet UILabel *label;
@end
@implementation ViewController
-(void)viewDidLoad{
[super
viewDidLoad];
//使用alloc init实例化的按钮,就是custom类型的,按钮的类型一旦指定,就不能修改
//UIButton *btn =[[UIButton alloc]initWithFrame:CGRectMake(100, 100, 199, 100)];
UIButton *btn = [UIButton
buttonWithType:UIButtonTypeCustom];
self.btn = btn;
//添加点击事件
[btn addTarget:self
action:@selector(printPersonInfo:)
forControlEvents:UIControlEventTouchUpInside];
[btn setFrame:CGRectMake(100,
100, 100,
100)];
[btn setBackgroundImage:[UIImage
imageNamed:@"btn_01"]
forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage
imageNamed:@"btn_02"]
forState:UIControlStateHighlighted];
[btn setTitle:@"点我"
forState:UIControlStateNormal];
[btn setTitle:@"别点我"
forState:UIControlStateHighlighted];
[btn setTitleColor:[UIColor
redColor] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor
greenColor] forState:UIControlStateHighlighted];
btn.contentVerticalAlignment =
UIControlContentVerticalAlignmentBottom;
[self.view
addSubview:btn];
UILabel *label = [[UILabel
alloc]initWithFrame:CGRectMake(100,
300, 300,
50)];
self.label = label;
[self.view
addSubview:label];
}
-(IBAction)printPersonInfo:(UIButton *)button{
self.label.text
= @"我是JS,喜欢编程!";
}
@end
相关文章推荐
- Java读写Excel文件示例
- java socket传送和接受byte[]
- 一晚上将一个模板整合进了DJANGO
- 将MATLAB中的mat数据转换为OpenCV能够读取的xml数据
- Java类集学习(六)应用范例(一对多的关系)
- eclipse jad 反编译 插件安装
- C++11实现一个自动注册的工厂
- Hackerearth.com编程问题解题思路系列:Roy's Chocolates
- javaWeb之Servlet的工作原理与生命周期
- byte[]转为string,再用substring取值,在eclipse和exe里取得值不同
- windows下配置eclipse + Python + PyDev + tornado,并使用pylint进行静态检测
- [一]初识SpringMVC
- java的内存管理机制
- Java创建线程的两个方法
- halcon与C++的交互
- 1. 【创建与销毁对象】考虑用静态工厂方法代替构造器
- ERROR Executor: Exception in task 0.0 in stage 0.0 (TID 0) java.lang.ClassNotFoundException: main.s
- Google和eBay在建设微服务生态系统中的深刻教训
- 模板元编程
- 【cas】keytool error: java.io.FileNotFoundException: xxx (Is a directory)