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

iPhone應(yīng)用程序 Delegate使用方法詳解

移動(dòng)開發(fā) iOS
本文介紹的是iPhone應(yīng)用程序 Delegate使用方法詳解,本文通過一個(gè)例子來學(xué)習(xí)Delegate的使用方法,先來看內(nèi)容。

iPhone應(yīng)用程序 Delegate使用方法詳解是本文要介紹的內(nèi)容,通過一個(gè)實(shí)例讓我們快速的去學(xué)習(xí),不多說,我們先來看內(nèi)容。

先舉一個(gè)例子:

假如"我"的本職工作之一是“接電話”,但"我"發(fā)現(xiàn)太忙了或來電太雜了,于是我聘請一位"秘書"分擔(dān)我“接電話”的工作,如果電話是老板打來的,就讓“秘書”將電話轉(zhuǎn)接給“我”。。。

那么,“我”就是A Object. “秘書”就是"我"的“Delegate”。寫成代碼就是 -- [我 setDelegate:秘書];

delegate的概念出現(xiàn)與mvc(model-view-controller),protocol,單線繼承 密切相關(guān)

  1. The main value of delegation is that it allows you to easily customize the behavior of several objects in one central object. 

Cocoa 中處理事件的方式有幾種,其中一種是你可以重載類中的對應(yīng)的事件處理方 法,比如MouseDown事件在NSResponse類中就被方法mouseDown:處理,所以所有繼承自NSResponse的類都可以重載 mouseDown:方法來實(shí)現(xiàn)對MouseDown事件的處理。

另外一種處理方式就是使用Delegate,當(dāng)一個(gè)對象接受到某個(gè)事件或者通知的時(shí)候, 會(huì)向它的Delegate對象查詢它是否能夠響應(yīng)這個(gè)事件或者通知,如果可以這個(gè)對象就會(huì)給它的Delegate對象發(fā)送一個(gè)消息(執(zhí)行一個(gè)方法調(diào)用)

協(xié)議 Protocol :

我說下我的理解。object-c 里沒有多繼承。那么又要避免做出一個(gè)對象什么都會(huì)(super class monster,huge ,super,waste)一個(gè)超能對象 本身是否定了面向?qū)ο蟮母拍詈驼嬷B了。為了讓代碼更簡潔,條理更清楚,可以將部分職責(zé)分離。

協(xié)議本身沒有具體的實(shí)現(xiàn)。只規(guī)定了一些可以被其它類實(shí)現(xiàn)的接口。

  1. @protocal UITextFieldDelegate    
  2. -(BOOL) textFieldShouldReturn:(UITextField *) textField ;    
  3. @end    
  4. @protocal UITextFieldDelegate  
  5. -(BOOL) textFieldShouldReturn:(UITextField *) textField ;  
  6. @end 

delegate 總是被定義為 assign @property

  1. @interface UITextField    
  2. @property (assign) id<UITextFieldDelegate> delegate;    
  3. @end    
  4. @interface UITextField  
  5. @property (assign) id<UITextFieldDelegate> delegate;  
  6. @end  

這樣我們就在UITextField內(nèi)部聲明一個(gè)委托(delegate),那么就需要委托的代理實(shí)現(xiàn)UITextFieldDelegate 中約定的行為

  1. // 首先, 在接口里邊聲明要使用誰的Delegate     
  2. @interface delegateSampleViewController : UIViewController    
  3.     <UITextFieldDelegate> {}    
  4. @end    
  5.      
  6. // 然后在實(shí)現(xiàn)文件中初始化的時(shí)候, 設(shè)置Delegate為self(自己)     
  7. @implementation delegateSampleViewController    
  8.      
  9. // ....     
  10.     UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 300, 400)];    
  11.     textField.delegate = self;//設(shè)置當(dāng)前的控制器為UITextField的代理,相當(dāng)于注冊(指定)代理人     
  12.     [textField becomeFirstResponder];    
  13.     [cell.contentView addSubview:textField];    
  14.     [textField release];    
  15. // ....     
  16.      
  17. }    
  18.      
  19. // 實(shí)現(xiàn)UITextFieldDelegate中約定的行為     
  20. #pragma mark UITextFieldDelegate Method     
  21. // called when 'return' key pressed. return NO to ignore.     
  22. - (BOOL)textFieldShouldReturn:(UITextField *)textField {    
  23.      
  24.     [textField resignFirstResponder];    
  25.     return YES;    
  26. }  

小結(jié):iPhone應(yīng)用程序 Delegate使用方法詳解的內(nèi)容介紹完了,希望本文讀你有所幫助!

責(zé)任編輯:zhaolei 來源: 互聯(lián)網(wǎng)
相關(guān)推薦

2011-08-05 14:58:58

iPhone CoreAnimat 動(dòng)畫

2011-07-26 09:41:23

iPhone xcode Mac OS X

2011-07-19 14:36:32

iPhone

2011-08-10 16:08:02

iPhoneProtocol協(xié)議

2011-08-10 16:50:10

iPhone生命周期

2011-07-21 10:47:37

iPhone Cocoa 委托

2011-08-16 16:40:06

iPhone應(yīng)用正則表達(dá)式OgreKit

2023-08-07 09:01:42

Prism 庫開源

2011-08-08 14:07:49

iPhone開發(fā) 字體

2011-08-03 17:27:40

iPhone UIScrollVi

2011-08-10 16:57:15

iPhone事件程序包

2011-08-10 17:30:50

iphoneThree20

2012-04-26 13:48:56

iPhone應(yīng)用發(fā)布Ad Hoc

2011-07-29 14:08:26

iPhone UTF-8 XML

2011-07-26 10:09:08

iPhone 多語言 國際化

2011-05-24 10:30:31

PythonNetBeans ID

2022-05-14 23:51:31

云計(jì)算安全混合云

2011-07-20 15:58:58

iPhone 應(yīng)用程序 生命周期

2010-08-27 10:41:41

iPhone核心應(yīng)用程序

2011-07-27 17:30:40

iPhone Locate 定位
點(diǎn)贊
收藏

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