self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
self.window.rootViewController = [[UIViewController alloc] init];
//UIButton
//1.創(chuàng)建(便利構(gòu)造器)
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
//2.設(shè)置frame
btn.frame = CGRectMake(100, 100, 100, 100);
//3.設(shè)置屬性
btn.backgroundColor = [UIColor redColor];
//4.綁定按鈕點(diǎn)擊事件(按鈕點(diǎn)擊時(shí) 能夠觸發(fā)一個(gè)方法)
//參數(shù)一:Target 目標(biāo)(調(diào)用方法的人) 參數(shù)二:action 動(dòng)作(調(diào)用的方法) 參數(shù)三:Events 事件(方法的觸發(fā)條件)
//UIControlEventTouchUpInside 控制事件之 觸摸頂端按下去
// : 參數(shù)標(biāo)志(之后一定會(huì)跟隨一個(gè)參數(shù))
[btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
//5.添加父視圖
[self.window addSubview:btn];
//按鈕文字
//參數(shù)1: 標(biāo)題內(nèi)容? 參數(shù)2: 狀態(tài)
[btn setTitle:@"正常" forState:UIControlStateNormal];
//按鈕長(zhǎng)按就是高亮狀態(tài)
[btn setTitle:@"高亮" forState:UIControlStateHighlighted];
//高亮?xí)r 按鈕顯示觸摸
btn.showsTouchWhenHighlighted = YES;
//建一個(gè)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(@"點(diǎn)點(diǎn)點(diǎn)");
//? ? NSLog(@"%@",sender);
if (sender.tag == 1000) {
NSLog(@"上上上");
}
if (sender.tag == 2000) {
NSLog(@"下下下");
}