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

淺談Objective-C協(xié)議和委托

移動開發(fā) iOS
Objective-C協(xié)議和委托是本文呢要介紹的內(nèi)容,主要介紹了Objective-C中協(xié)議和委托的方式,先來看詳細(xì)內(nèi)容。

Objective-C協(xié)議委托是本文呢要介紹的內(nèi)容,主要介紹了Objective-C協(xié)議委托的方式,通過實(shí)例講解讓我們更快更方便的去學(xué)習(xí)Objective-C,先來看詳細(xì)內(nèi)容。

protocol-協(xié)議,就是使用了這個協(xié)議后就要按照這個協(xié)議來辦事,協(xié)議要求實(shí)現(xiàn)的方法就一定要實(shí)現(xiàn)。

delegate-委托,顧名思義就是委托別人辦事,就是當(dāng)一件事情發(fā)生后,自己不處理,讓別人來處理。

當(dāng)一個A view 里面包含了B view

b view需要修改a view界面,那么這個時(shí)候就需要用到委托了。

需要幾個步驟

1、首先定一個協(xié)議

2、a view實(shí)現(xiàn)協(xié)議中的方法

3、b view設(shè)置一個委托變量

4、把b view的委托變量設(shè)置成a view,意思就是 ,b view委托a view辦事情。

5、事件發(fā)生后,用委托變量調(diào)用a view中的協(xié)議方法

例子:

  1. B_View.h:  
  2. @protocol UIBViewDelegate <NSObject> 
  3. @optional  
  4. - (void)ontouch:(UIScrollView *)scrollView; //聲明協(xié)議方法  
  5. @end  
  6.  
  7. @interface BView : UIScrollView<UIScrollViewDelegate>   
  8. {  
  9.  id< UIBViewDelegate > _touchdelegate; //設(shè)置委托變量  
  10. }  
  11. @property(nonatomic,assign) id< UIBViewDelegate > _touchdelegate;   
  12. @end  
  13.  
  14. B_View.mm:  
  15.  
  16. @synthesize _touchdelegate;  
  17. - (id)initWithFrame:(CGRect)frame {  
  18. if (self = [super initWithFrame:frame]) {  
  19.  // Initialization code  
  20.  _touchdelegate=nil;  
  21.  }  
  22.  return self;  
  23. }  
  24.  
  25. - (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event  
  26. {  
  27.  [super touchesBegan:touches withEvent:event];  
  28.  if(_touchdelegate!=nil && [_touchdelegate respondsToSelector: @selector(ontouch:) ] == true)   
  29.   [_touchdelegate ontouch:self];  //調(diào)用協(xié)議委托  
  30. }  
  31. @end  
  32.  
  33. A_View.h:  
  34.  
  35. @interface AViewController : UIViewController < UIBViewDelegate > 
  36. {  
  37.  BView *m_BView;  
  38. }  
  39. @end  
  40.  
  41. A_View.mm:  
  42.  
  43. - (void)viewWillAppear:(BOOL)animated  
  44. {  
  45.  m_BView._touchdelegate = self; //設(shè)置委托  
  46.  [self.view addSubview: m_BView];  
  47. }  
  48.  
  49. - (void)ontouch:(UIScrollView *)scrollView  
  50. {  
  51.     //實(shí)現(xiàn)協(xié)議  

小結(jié):淺談Objective-C協(xié)議委托的內(nèi)容介紹完了,希望本文對你有所幫助!

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

2011-08-15 14:32:42

Objective-C委托協(xié)議

2011-07-25 10:03:06

Objective-C 委托

2011-08-03 16:55:05

Objective-C 代理

2011-08-04 09:35:09

Objective-C 編碼規(guī)范

2011-08-02 13:16:36

Objective-C 語法 函數(shù)

2011-08-04 11:15:46

Objective-C 構(gòu)造函數(shù) 構(gòu)造方法

2011-07-25 13:05:37

Objective-C 委托

2013-04-11 13:41:30

Objective-CiOS編程

2011-07-27 16:18:42

Objective-c 協(xié)議

2011-07-28 18:11:18

Objective-C Cocoa 編程

2010-09-10 14:15:19

daytime協(xié)議時(shí)間協(xié)議

2011-08-04 10:04:17

Objective-C 分類 協(xié)議

2011-05-11 15:58:34

Objective-C

2013-03-27 12:54:00

iOS開發(fā)Objective-C

2013-06-20 10:40:32

Objective-C實(shí)現(xiàn)截圖

2011-05-11 11:20:26

Objective-C

2011-08-10 18:07:29

Objective-C反射

2011-07-25 14:27:10

Objective-C 協(xié)議 函數(shù)

2011-07-08 13:49:46

Objective-C UUID

2011-07-29 16:16:30

Objective-c block
點(diǎn)贊
收藏

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