iPhone應用程式 Delegate使用方法詳解

來源:互聯網
上載者:User

iPhone應用程式 Delegate使用方法詳解是本文要介紹的內容,通過一個執行個體讓我們快速的去學習,不多說,我們先來看內容。

先舉一個例子:

假如"我"的本職工作之一是“接電話”,但"我"發現太忙了或來電太雜了,於是我聘請一位"秘書"分擔我“接電話”的工作,如果電話是老闆打來的,就讓“秘書”將電話轉接給“我”。。。

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

delegate的概念出現與mvcmodel-view-controller),protocol,單線繼承 密切相關

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

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

另外一種處理方式就是使用Delegate,當一個對象接受到某個事件或者通知的時候, 會向它的Delegate物件查詢它是否能夠響應這個事件或者通知,如果可以這個對象就會給它的Delegate對象發送一個訊息執行一個方法調用)

協議 Protocol :

我說下我的理解。object-c 裡沒有多繼承。那麼又要避免做出一個對象什麼都會super class monster,huge ,super,waste)一個超能對象 本身是否定了物件導向的概念和真諦了。為了讓代碼更簡潔,條理更清楚,可以將部分職責分離。

協議本身沒有具體的實現。只規定了一些可以被其它類實現的介面。

 
  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內部聲明一個委託(delegate),那麼就需要委託的代理實現UITextFieldDelegate 中約定的行為

 
  1. // 首先, 在介面裡邊聲明要使用誰的Delegate     
  2. @interface delegateSampleViewController : UIViewController    
  3.     <UITextFieldDelegate> {}    
  4. @end    
  5.      
  6. // 然後在實現檔案中初始化的時候, 設定Delegate為self(自己)     
  7. @implementation delegateSampleViewController    
  8.      
  9. // ....     
  10.     UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 300, 400)];    
  11.     textField.delegate = self;//設定當前的控制器為UITextField的代理,相當於註冊(指定)代理人     
  12.     [textField becomeFirstResponder];    
  13.     [cell.contentView addSubview:textField];    
  14.     [textField release];    
  15. // ....     
  16.      
  17. }    
  18.      
  19. // 實現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. }  

小結:iPhone應用程式 Delegate使用方法詳解的內容介紹完了,希望本文讀你有所協助!

聯繫我們

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