iPhone開(kāi)發(fā)進(jìn)階(4)編程定制UIButton案例實(shí)現(xiàn)
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。
- @interface CustomViewController : UIViewController {
 - int count; // 計(jì)數(shù)器變量。
 - }
 - @end
 
接下來(lái),添加按鈕按下時(shí)調(diào)用的方法。
- -(void)countup:(id)inSender {
 - count++; // 計(jì)數(shù)器自加
 - // inSender 是被點(diǎn)擊的 Button 的實(shí)例,下面設(shè)置其標(biāo)題
 - [inSender setTitle:[NSString
 - stringWithFormat:@"count:%d", count]
 - forState:UIControlStateNormal];
 - }
 
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 類,所以可以直接使用)。如下所示:
- - (void)viewDidLoad {
 - [super viewDidLoad];
 - self.view.backgroundColor = [UIColor blueColor];
 - UIButton* button = [UIButton buttonWithType:UIButtonTypeInfoLight];
 - button.frame = CGRectMake(100,100,100,100);
 - // 注冊(cè)按鈕按下時(shí)的處理函數(shù)
 - [button addTarget:self action:@selector(countup:)
 - forControlEvents:UIControlEventTouchUpInside];
 - [self.view addSubview:button];
 
forControlEvents: 中設(shè)定 UIControlEventTouchUpInside 是指在按鈕上按下時(shí)響應(yīng)。
因?yàn)閯?dòng)作函數(shù)(countup)的類型是
- -(void)countup:(id)inSender
 
則在注冊(cè)的時(shí)候需要寫(xiě) countup: 。
而如果函數(shù)類型是
- -(void)countup
 
的話,則是 countup ,這時(shí) addTarget 接收的函數(shù)類型如下所示:
- - (void) countup:(id)sender forEvent:(UIEvent *)event
 
同一響應(yīng),也可以注冊(cè)多個(gè)處理,比如下面的代碼,將上面兩種類型的動(dòng)作函數(shù)都注冊(cè)了:
- // ***種處理方法
 - -(void)countup:(id)inSender {
 - count++;
 - [inSender setTitle:[NSString
 - stringWithFormat:@"count:%d", count]
 - forState:UIControlStateNormal];
 - }
 - // 第二種處理方法
 - -(void)countup {
 - count++;
 - }
 - -(void)countup:(id)inSender forEvent:(UIEvent *)event {
 - count++;
 - [inSender setTitle:[NSString
 - stringWithFormat:@"count:%d", count]
 - forState:UIControlStateNormal];
 - }
 - - (void)viewDidLoad {
 - [super viewDidLoad];
 - self.view.backgroundColor = [UIColor blueColor];
 - UIButton* button = [UIButton buttonWithType:UIButtonTypeInfoLight];
 - button.frame = CGRectMake(100,100,100,100);
 - // 注冊(cè)***種方法
 - [button addTarget:self action:@selector(countup:)
 - forControlEvents:UIControlEventTouchUpInside];
 - // 注冊(cè)第二種方法
 - [button addTarget:self action:@selector(countup)
 - forControlEvents:UIControlEventTouchUpInside];
 - [button addTarget:self action:@selector(countup:forEvent:)
 - forControlEvents:UIControlEventTouchUpInside];
 - [self.view addSubview:button];
 - }
 
編譯以后,顯示如下:
小結(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)
















 
 
 


 
 
 
 