自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

iPhone開發(fā)進階(4)編程定制UIButton案例實現(xiàn)

移動開發(fā) iOS
iPhone開發(fā)編程定制UIButton案例實現(xiàn)是本文要介紹的內(nèi)容,主要是來講述一下自動創(chuàng)建 UIButton 而不使用 XIB 文件,來看具體內(nèi)容。

iPhone開發(fā)編程定制UIButton案例實現(xiàn)是本文要介紹的內(nèi)容,主要是來講述一下自動創(chuàng)建 UIButton 而不使用 XIB 文件。通過這一節(jié)的學(xué)習(xí),我們可以掌握不通過 XIB (InterfaceBuilder) 來使用 UIControl 的 addTarget 方法、對應(yīng)相應(yīng)的事件動作。

具體的例子是基于上篇CustomViewController 類,并且按鈕按下是計數(shù)器加一,并顯示在視圖上。

首先,在 CustomViewController 類中添加技術(shù)用的變量 count。

  1. @interface CustomViewController : UIViewController {  
  2.     int count;  // 計數(shù)器變量。  
  3. }  
  4. @end  

接下來,添加按鈕按下時調(diào)用的方法。

  1.  -(void)countup:(id)inSender {  
  2.     count++;                        //  計數(shù)器自加  
  3.     //  inSender 是被點擊的 Button 的實例,下面設(shè)置其標題  
  4.     [inSender setTitle:[NSString  
  5.         stringWithFormat:@"count:%d", count]  
  6.         forState:UIControlStateNormal];  
  7. }  

setTitle 方法設(shè)定 UIButton 的標題。使用 forState: 來指定該標題顯示的狀態(tài)(按下,彈起,通常),這里指定通常狀態(tài)顯示的標題。當然,使用 UIControlStateNormal 也是可以的。

注冊按鈕按下時的事件函數(shù)可以通過 UIControl 類中的 addTarget:action:forControlEvents: 方法(UIButton 繼承了UIControl 類,所以可以直接使用)。如下所示:

  1. - (void)viewDidLoad {  
  2.    [super viewDidLoad];  
  3.    self.view.backgroundColor = [UIColor blueColor];  
  4.    UIButton* button = [UIButton buttonWithType:UIButtonTypeInfoLight];  
  5.    button.frame = CGRectMake(100,100,100,100);  
  6.    // 注冊按鈕按下時的處理函數(shù)  
  7.    [button addTarget:self action:@selector(countup:)  
  8.        forControlEvents:UIControlEventTouchUpInside];  
  9.    [self.view addSubview:button];  
  10.   

forControlEvents: 中設(shè)定 UIControlEventTouchUpInside 是指在按鈕上按下時響應(yīng)。

因為動作函數(shù)(countup)的類型是

  1. -(void)countup:(id)inSender  

則在注冊的時候需要寫 countup: 。

而如果函數(shù)類型是

  1. -(void)countup  

的話,則是 countup ,這時 addTarget 接收的函數(shù)類型如下所示:

  1. - (void) countup:(id)sender forEvent:(UIEvent *)event  

同一響應(yīng),也可以注冊多個處理,比如下面的代碼,將上面兩種類型的動作函數(shù)都注冊了:

  1. // ***種處理方法  
  2. -(void)countup:(id)inSender {  
  3.     count++;  
  4.     [inSender setTitle:[NSString  
  5.         stringWithFormat:@"count:%d", count]  
  6.         forState:UIControlStateNormal];  
  7. }  
  8.  
  9. // 第二種處理方法  
  10. -(void)countup {  
  11.     count++;  
  12. }  
  13.  
  14. -(void)countup:(id)inSender forEvent:(UIEvent *)event {  
  15.     count++;  
  16.     [inSender setTitle:[NSString  
  17.         stringWithFormat:@"count:%d", count]  
  18.         forState:UIControlStateNormal];  
  19. }  
  20.  
  21. - (void)viewDidLoad {  
  22.     [super viewDidLoad];  
  23.     self.view.backgroundColor = [UIColor blueColor];  
  24.     UIButton* button = [UIButton buttonWithType:UIButtonTypeInfoLight];  
  25.     button.frame = CGRectMake(100,100,100,100);  
  26.     // 注冊***種方法  
  27.     [button addTarget:self action:@selector(countup:)  
  28.         forControlEvents:UIControlEventTouchUpInside];  
  29.     // 注冊第二種方法  
  30.     [button addTarget:self action:@selector(countup)  
  31.         forControlEvents:UIControlEventTouchUpInside];  
  32.     [button addTarget:self action:@selector(countup:forEvent:)  
  33.         forControlEvents:UIControlEventTouchUpInside];  
  34.     [self.view addSubview:button];  
  35. }  

編譯以后,顯示如下:

編程定制UIButton案例實現(xiàn)

小結(jié):iPhone開發(fā)編程定制UIButton案例實現(xiàn)的內(nèi)容介紹完了,希望通過本文的學(xué)習(xí)能對你有所幫助!如果想繼續(xù)深入了解的話,請參考以下幾篇文章:

iPhone開發(fā)進階(1)iPhone應(yīng)用程序項目構(gòu)成案例實現(xiàn)

iPhone開發(fā)進階(2)iPhone應(yīng)用程序的啟動過程

iPhone開發(fā)進階(3)編程定制UIViewController案例實現(xiàn)

責(zé)任編輯:zhaolei 來源: 博客園
相關(guān)推薦

2011-08-17 16:23:31

iPhone開發(fā)UIViewContr

2011-08-17 16:12:20

iPhone應(yīng)用程序

2011-08-17 16:16:29

iPhone應(yīng)用程序啟動過程

2011-05-03 15:28:15

BlackBerryWidget

2013-12-27 09:54:58

Android開發(fā)NDK

2011-08-15 15:44:46

iPhone開發(fā)PDF

2011-08-18 16:24:44

iPhone開發(fā)圖片

2010-12-23 09:11:17

讀寫Android文件

2012-02-07 10:05:40

jQuery MobijQuery Mobi

2011-08-19 11:10:31

iPhone應(yīng)用

2011-08-19 10:13:05

iPhone開發(fā)

2011-08-16 15:48:37

iPhone開發(fā)抓圖程序

2011-08-18 15:24:40

iPhone國際化

2021-01-20 08:16:06

異步Dotnet Core多路徑

2023-08-01 08:52:03

WebRTC.Net線程

2011-08-19 10:05:30

iPhone開發(fā)

2014-01-07 14:53:37

Android開發(fā)依賴注入Roboguice

2011-07-29 14:18:46

iPhone開發(fā) 動畫

2011-10-18 10:17:39

Android應(yīng)用開發(fā)

2011-10-18 10:25:08

Android應(yīng)用開發(fā)
點贊
收藏

51CTO技術(shù)棧公眾號