iPhone開發之UIButton的用法

來源:互聯網
上載者:User

兩種方式建立UIButton的對象。

(1)

UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(100, 50, 100, 75)];
[button setTitle:@"close" forState:UIControlStateNormal];
button.backgroundColor = [UIColor greenColor];//button的背景顏色
[button setBackgroundImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];//button的背景圖片

(2)
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button1.frame = CGRectMake(200, 20, 50, 60);
button1.backgroundColor = [UIColor blackColor];
[button1 setTitle:@"clicke" forState:UIControlStateNormal];
[self.window addSubview:button];
[self.window addSubview:button1];


注意: 下面建立UIButton的對象button不能在window的視窗中能顯示

UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(100, 50, 100, 75)];

button = [UIButton buttonWithType:UIButtonTypeRoundedRect];//多餘
[button setTitle:@"close" forState:UIControlStateNormal];
button.backgroundColor = [UIColor greenColor];//button的背景顏色
[button setBackgroundImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];//button的背景圖片

解釋 :

1、你先用alloc方式建立了button後,接著用靜態方法把button初始化了一次,就丟失了frame的資訊,並且還存在記憶體泄露的問題,第一次alloc產生的記憶體成為了無法回收的部分。2. UIButton比較特殊,並沒有實現initWithFrame這個初始化方法,這個方法是它父類的父類UIView中的方法,並不能讓它成為一個按鈕,如果你是添加一個UIView進來的話改掉我說的第一個問題後倒是可以成功的。因此UIButton還是得用你的第一種方式來進行初始化,然後通過設定frame來確定它的位置即可。
UIButton的詳細用法如下:

//這裡建立一個圓角矩形的按鈕

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

// 能夠定義的button類型有以下6種,

// typedef enum {

// UIButtonTypeCustom = 0, 自訂風格

// UIButtonTypeRoundedRect, 圓角矩形

// UIButtonTypeDetailDisclosure, 藍色小箭頭按鈕,主要做詳細說明用

// UIButtonTypeInfoLight, 亮色驚嘆號

// UIButtonTypeInfoDark, 暗色驚嘆號

// UIButtonTypeContactAdd, 十字加號按鈕

// } UIButtonType;

//給定button在view上的位置

button.frame = CGRectMake(20, 20, 280, 40);

//隱藏button按鈕

button.hidden = !button.hidden

//button背景色

button.backgroundColor = [UIColor clearColor];

//設定button背景圖片

[button setBackgroundImage:[UIImage imageNamed:@"PIC"] forState:UIControlStateHighlighted];//背景映像

//設定button填充圖片

[button setImage:[UIImage imageNamed:@"btng.png"] forState:UIControlStateNormal];

//設定button標題

[button setTitle:@"點擊" forState:UIControlStateNormal];

/* forState: 這個參數的作用是定義按鈕的文字或圖片在何種狀態下才會顯現*/

//以下是幾種狀態

// enum {

// UIControlStateNormal = 0, 常規狀態顯現

// UIControlStateHighlighted = 1 << 0, 高亮狀態顯現

// UIControlStateDisabled = 1 << 1, 禁用的狀態才會顯現

// UIControlStateSelected = 1 << 2, 選中狀態

// UIControlStateApplication = 0x00FF0000, 當應用程式標誌時

// UIControlStateReserved = 0xFF000000 為內部架構預留,可以不管他

// };

/*預設情況下,當按鈕高亮的情況下,映像的顏色會被畫深一點,如果這下面的這個屬性設定為no,那麼可以去掉這個功能*/

button.adjustsImageWhenHighlighted = NO;

/*跟上面的情況一樣,預設情況下,當按鈕禁用的時候,映像會被畫得深一點,設定NO可以取消設定*/

button.adjustsImageWhenDisabled = NO;

/* 下面的這個屬性設定為yes的狀態下,按鈕按下會發光*/

button.showsTouchWhenHighlighted = YES;

/* 給button添加事件,事件有很多種,我會單獨開一篇博文介紹它們,下面這個時間的意思是按下按鈕,並且手指離開螢幕的時候觸發這個事件,跟web中的click事件一樣。觸發了這個事件以後,執行butClick:這個方法,addTarget:self 的意思是說,這個方法在本類中也可以傳入其他類的指標*/

[button addTarget:self action:@selector(butClick:) forControlEvents:UIControlEventTouchUpInside];

//顯示控制項

[self.view addSubview:button1];

注意:

[button addTarget:self action:@selector(alarmTimeDone:)

forControlEvents:UIControlEventTouchUpInside];

addTarget:self 是連結到self,一般都這樣設定
action:@selector(alarmTimeDone:) 時間處理函數
forControlEvents:UIControlEventTouchUpInside 控制項事件處理的訊息

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.