偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

iPhone開(kāi)發(fā)進(jìn)階(4)編程定制UIButton案例實(shí)現(xiàn)

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

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

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

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

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

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

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

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

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

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

因?yàn)閯?dòng)作函數(shù)(countup)的類型是

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

則在注冊(cè)的時(shí)候需要寫(xiě) countup: 。

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

  1. -(void)countup  

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

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

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

  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.     // 注冊(cè)***種方法  
  27.     [button addTarget:self action:@selector(countup:)  
  28.         forControlEvents:UIControlEventTouchUpInside];  
  29.     // 注冊(cè)第二種方法  
  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案例實(shí)現(xiàn)

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

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

iPhone開(kāi)發(fā)進(jìn)階(2)iPhone應(yīng)用程序的啟動(dòng)過(guò)程

iPhone開(kāi)發(fā)進(jìn)階(3)編程定制UIViewController案例實(shí)現(xiàn)

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

2011-08-17 16:23:31

iPhone開(kāi)發(fā)UIViewContr

2011-08-17 16:12:20

iPhone應(yīng)用程序

2011-08-17 16:16:29

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

2011-05-03 15:28:15

BlackBerryWidget

2013-12-27 09:54:58

Android開(kāi)發(fā)NDK

2011-08-18 16:24:44

iPhone開(kāi)發(fā)圖片

2011-08-15 15:44:46

iPhone開(kāi)發(fā)PDF

2010-12-23 09:11:17

讀寫(xiě)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開(kāi)發(fā)

2011-08-16 15:48:37

iPhone開(kāi)發(fā)抓圖程序

2011-08-18 15:24:40

iPhone國(guó)際化

2021-01-20 08:16:06

異步Dotnet Core多路徑

2011-08-19 10:05:30

iPhone開(kāi)發(fā)

2023-08-01 08:52:03

WebRTC.Net線程

2011-07-29 14:18:46

iPhone開(kāi)發(fā) 動(dòng)畫(huà)

2014-01-07 14:53:37

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

2011-10-18 10:17:39

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

2011-10-18 10:25:08

Android應(yīng)用開(kāi)發(fā)
點(diǎn)贊
收藏

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