UIButton

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

self.window.backgroundColor = [UIColor whiteColor];

[self.window makeKeyAndVisible];

self.window.rootViewController = [[UIViewController alloc] init];

//UIButton

//1.创建(便利构造器)

UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];

//2.设置frame

btn.frame = CGRectMake(100, 100, 100, 100);

//3.设置属性

btn.backgroundColor = [UIColor redColor];

//4.绑定按钮点击事件(按钮点击时 能够触发一个方法)

//参数一:Target 目标(调用方法的人) 参数二:action 动作(调用的方法) 参数三:Events 事件(方法的触发条件)

//UIControlEventTouchUpInside 控制事件之 触摸顶端按下去

// : 参数标志(之后一定会跟随一个参数)

[btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

//5.添加父视图

[self.window addSubview:btn];

//按钮文字

//参数1: 标题内容  参数2: 状态

[btn setTitle:@"正常" forState:UIControlStateNormal];

//按钮长按就是高亮状态

[btn setTitle:@"高亮" forState:UIControlStateHighlighted];

//高亮时 按钮显示触摸

btn.showsTouchWhenHighlighted = YES;

//建一个button 绑定click:

UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeSystem];

btn1.frame = CGRectMake(100, 300, 100, 100);

btn1.backgroundColor = [UIColor yellowColor];

[btn1 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

[self.window addSubview:btn1];

[btn1 setTitle:@"正常" forState:UIControlStateNormal];

[btn1 setTitle:@"高亮" forState:UIControlStateHighlighted];

btn1.showsTouchWhenHighlighted = YES;

btn.tag = 1000;

btn1.tag = 2000;

[_window release];

return YES;

}

//-(void)click:(id)sender{

-(void)click:(UIButton *)sender{

sender.backgroundColor = [UIColor colorWithHue:arc4random() % 256 / 255.0 saturation:arc4random() % 256 / 255.0 brightness:arc4random() % 256 / 255.0 alpha:1];

//    NSLog(@"点点点");

//    NSLog(@"%@",sender);

if (sender.tag == 1000) {

NSLog(@"上上上");

}

if (sender.tag == 2000) {

NSLog(@"下下下");

}

你可能感兴趣的:(UIButton)