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.創(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.綁定按鈕點擊事件(按鈕點擊時 能夠觸發(fā)一個方法)

//參數(shù)一:Target 目標(調(diào)用方法的人) 參數(shù)二:action 動作(調(diào)用的方法) 參數(shù)三:Events 事件(方法的觸發(fā)條件)

//UIControlEventTouchUpInside 控制事件之 觸摸頂端按下去

// : 參數(shù)標志(之后一定會跟隨一個參數(shù))

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

//5.添加父視圖

[self.window addSubview:btn];

//按鈕文字

//參數(shù)1: 標題內(nèi)容? 參數(shù)2: 狀態(tài)

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

//按鈕長按就是高亮狀態(tài)

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

//高亮?xí)r 按鈕顯示觸摸

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(@"下下下");

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容