建立一個button 并添加對象
//建立一個圓角button
UIButton*btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame=CGRectMake(100, 100, 80, 40);
[btn setTitle:@"按鈕" forState:UIControlStateNormal];
//向按鈕添加事件
//p1 addTarget誰來實作這個事件函數
//p2 @selector(pressBtn)
//p3 事件類型 forControlEvents UIControlEventTouchUpInside 事件處理函數類型
//UIControlEventTouchUpInside 點選按鈕彈起事件
//UIControlEventTouchDown 當我們的手指觸碰到螢幕上時
[btn addTarget:self action:@selector(pressBtn) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
事件實作方法
-(void)pressBtn
{
NSLog(@"12454");
}
事件 帶參數
[btn addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
實作的方法
//帶參數函數 參數為按鈕本身
-(void)pressBtn:(UIButton*)btn
{
NSLog(@"%@",btn);
}
//
// ViewController.m
// les11
//
// Created by 易飛 on 2019/3/26.
// Copyright © 2019 yifei. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
//建立ui控件
-(void)createRectButton
{
//建立一個btn對象,根據類型來建立btn
//btn類型圓角類型 btn UIButtonTypeRoundedRect
//通過類方法來建立 l記憶體自己管理
UIButton* btn =[UIButton buttonWithType:UIButtonTypeRoundedRect];
//設定btn的位置
btn.frame =CGRectMake(100, 100, 100, 40);
//設定按鈕的文字内容
//@parameter
//p1:字元串類型,顯示到按鈕上的文字
//p2 設定文字顯示的狀态類型 UIControlStateNormal 正常狀态 UIControlStateHighlighted 高亮狀态
[btn setTitle:@"按鈕01" forState:UIControlStateNormal];
[btn setTitle:@"按鈕按下" forState:UIControlStateHighlighted];
btn.backgroundColor = [UIColor grayColor]; //背景顔色
//設定文字顔色 正常狀态下的顔色 p1 顔色 p2狀态
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
//設定按鈕的風格顔色 優先級比setTitleColor 低
[btn setTintColor:[UIColor whiteColor]];
//UILable控件
btn.titleLabel.font = [UIFont systemFontOfSize:30];//設定btn的文字大小
[self.view addSubview:btn];//添加到視圖
}
-(void)createImageButton
{
//建立一個自定義類型的button UIButtonTypeCustom
UIButton*btnImage = [UIButton buttonWithType:UIButtonTypeCustom];
btnImage.frame= CGRectMake(100, 200, 100, 100);
UIImage*icon01 =[UIImage imageNamed:@"btn01"];
UIImage*icon02 = [UIImage imageNamed:@"btn02"];
[btnImage setImage:icon01 forState:UIControlStateNormal];
[btnImage setImage:icon02 forState:UIControlStateHighlighted];
[self.view addSubview:btnImage];
}
-(void)createButton
{
//建立一個圓角button
UIButton*btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame=CGRectMake(100, 100, 80, 40);
[btn setTitle:@"按鈕" forState:UIControlStateNormal];
//向按鈕添加事件
//p1 addTarget誰來實作這個事件函數
//p2 @selector(pressBtn)
//p3 事件類型 forControlEvents UIControlEventTouchUpInside 事件處理函數類型
//UIControlEventTouchUpInside 點選按鈕彈起事件
//UIControlEventTouchDown 當我們的手指觸碰到螢幕上時
[btn addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside];
//觸碰時調用事件函數
[btn addTarget:self action:@selector(touhDown) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:btn];
UIButton*btn02 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn02.frame= CGRectMake(100, 200, 80, 40);
[btn02 setTitle:@"按鈕02" forState:UIControlStateNormal];
[btn02 addTarget:self action:@selector(pressBtn02) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn02];
//設定按鈕的标記值
btn.tag = 101;
btn.tag =102;
}
-(void)pressBtn02
{
NSLog(@"bbbb");
}
-(void)touhDown
{
NSLog(@"觸碰");
}
//普通函數
-(void)pressBtn
{
NSLog(@"12454");
}
//帶參數函數
-(void)pressBtn:(UIButton*)btn
{
NSLog(@"%@",btn);
}
- (void)viewDidLoad {
[super viewDidLoad];
[self createButton];
}
@end
最終版
//
// ViewController.m
// les11
//
// Created by 易飛 on 2019/3/26.
// Copyright © 2019 yifei. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
//建立ui控件
-(void)createRectButton
{
//建立一個btn對象,根據類型來建立btn
//btn類型圓角類型 btn UIButtonTypeRoundedRect
//通過類方法來建立 l記憶體自己管理
UIButton* btn =[UIButton buttonWithType:UIButtonTypeRoundedRect];
//設定btn的位置
btn.frame =CGRectMake(100, 100, 100, 40);
//設定按鈕的文字内容
//@parameter
//p1:字元串類型,顯示到按鈕上的文字
//p2 設定文字顯示的狀态類型 UIControlStateNormal 正常狀态 UIControlStateHighlighted 高亮狀态
[btn setTitle:@"按鈕01" forState:UIControlStateNormal];
[btn setTitle:@"按鈕按下" forState:UIControlStateHighlighted];
btn.backgroundColor = [UIColor grayColor]; //背景顔色
//設定文字顔色 正常狀态下的顔色 p1 顔色 p2狀态
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
//設定按鈕的風格顔色 優先級比setTitleColor 低
[btn setTintColor:[UIColor whiteColor]];
//UILable控件
btn.titleLabel.font = [UIFont systemFontOfSize:30];//設定btn的文字大小
[self.view addSubview:btn];//添加到視圖
}
-(void)createImageButton
{
//建立一個自定義類型的button UIButtonTypeCustom
UIButton*btnImage = [UIButton buttonWithType:UIButtonTypeCustom];
btnImage.frame= CGRectMake(100, 200, 100, 100);
UIImage*icon01 =[UIImage imageNamed:@"btn01"];
UIImage*icon02 = [UIImage imageNamed:@"btn02"];
[btnImage setImage:icon01 forState:UIControlStateNormal];
[btnImage setImage:icon02 forState:UIControlStateHighlighted];
[self.view addSubview:btnImage];
}
-(void)createButton
{
//建立一個圓角button
UIButton*btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame=CGRectMake(100, 100, 80, 40);
[btn setTitle:@"按鈕" forState:UIControlStateNormal];
//向按鈕添加事件
//p1 addTarget誰來實作這個事件函數
//p2 @selector(pressBtn)
//p3 事件類型 forControlEvents UIControlEventTouchUpInside 事件處理函數類型
//UIControlEventTouchUpInside 點選按鈕彈起事件
//UIControlEventTouchDown 當我們的手指觸碰到螢幕上時
[btn addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside];
//觸碰時調用事件函數
[btn addTarget:self action:@selector(touhDown) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:btn];
UIButton*btn02 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn02.frame= CGRectMake(100, 200, 80, 40);
[btn02 setTitle:@"按鈕02" forState:UIControlStateNormal];
[btn02 addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn02];
//設定按鈕的标記值
btn.tag = 101;
btn.tag =102;
}
-(void)pressBtn02
{
NSLog(@"bbbb");
}
-(void)touhDown
{
NSLog(@"觸碰");
}
//普通函數
-(void)pressBtn
{
NSLog(@"12454");
}
//帶參數函數
-(void)pressBtn:(UIButton*)btn
{
NSLog(@"%@",btn);
if(btn.tag==101){
NSLog(@"現在是101");
}else if(btn.tag==102){
NSLog(@"現在是102");
}
}
- (void)viewDidLoad {
[super viewDidLoad];
[self createButton];
}
@end