iPhone開發進階(4)編程定製UIButton案例實現

來源:互聯網
上載者:User

iPhone開發編程定製UIButton案例實現是本文要介紹的內容,主要是來講述一下自動建立 UIButton 而不使用 XIB 檔案。通過這一節的學習,我們可以掌握不通過 XIB (InterfaceBuilder) 來使用 UIControl 的 addTarget 方法、對應相應的事件動作。

具體的例子是基於上篇CustomViewController 類,並且按鈕按下是計數器加一,並顯示在視圖上。

首先,在 CustomViewController 類中添加技術用的變數 count。

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

接下來,添加按鈕按下時調用的方法。

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

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

註冊按鈕按下時的事件函數可以通過 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.    // 註冊按鈕按下時的處理函數  
  7.    [button addTarget:self action:@selector(countup:)  
  8.        forControlEvents:UIControlEventTouchUpInside];  
  9.    [self.view addSubview:button];  
  10.   

forControlEvents: 中設定 UIControlEventTouchUpInside 是指在按鈕上按下時響應。

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

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

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

而如果函數類型是

 
  1. -(void)countup  

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

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

同一響應,也可以註冊多個處理,比如下面的代碼,將上面兩種類型的動作函數都註冊了:

 
  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. }  

編譯以後,顯示如下:

小結:iPhone開發編程定製UIButton案例實現的內容介紹完了,希望通過本文的學習能對你有所協助!如果想繼續深入瞭解的話,請參考以下幾篇文章:

iPhone開發進階1)iPhone應用程式項目構成案例實現

iPhone開發進階2)iPhone應用程式的啟動過程

iPhone開發進階3)編程定製UIViewController案例實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.