您的位置:首页 > 编程语言

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: