[转]浅谈Objective-C代理方案 &&浅谈Objective-C协议和委托

http://blog.sina.com.cn/s/blog_6297d52d01010fuv.html

 

本文实现AView中点击切换到BView,BView中点击又切换回AView中,并把Bview删除或隐藏,这样的一个实例,来看内容。

AD:

Objective-C代理方案是本文要介绍的内容。通过一个ViewController控制着AView和BView,这样的一个实例来介绍内容,我们先来看详细内容。

现在要实现:AView中点击切换到BView,BView中点击又切换回AView中,并把Bview删除或隐藏。

由于对模式不是很清楚,所以没想到用什么模式处理,如果有什么好的模式可以解决上述问题,希望大侠们指教哇。

最后使用的方法是objective-c的代理。

1、在BView.h中定义一个代理:

  1. id delegate; // A delegate that wants to act on events in this view 

2、定义代理方法,这里使用到的是objective-c 的协议

  1. @interface NSObject ( BViewDelegate)   
  2. -(void)delegateMethod;  
  3. @end 

注:以上写的协议为非正式的,所以,不用被设置代理者,不实现这个代理方法也是可以的。

3、在点击中实现代码中,调用代理者实现的代理方法。在BView.m中添加如何类似

代码:

//设置了代理者,且代理者实现了

  1. delegateMethod  
  2. if(delegate && [delegate respondsToSelector:@selector(delegateMethod)])   
  3. delegate delegateMethod];  

4、在AViem.m中设置BView的代理,并实现delegateMethod方法即可:

类似初始化中添加:[aView setDelegate:self];

  1. -(void)delegateMethod{  
  2.       //control bView code here  
  3.       ...  

另外,关于正式协议:

  1. @protocol protocolName  
  2. @optional -(void)delegateMethodA  
  3. @required -(void)delegateMethodB  
  4. // other methods  
  5. ...  
  6. @end 

正式协议类似于java的借口或抽象类。@optional 的方法,可实现也可不实现,但@required 的方法必需实现。默认为@required。

正式协议要求在代理类中声明采纳此代理,如:

  1. @interface ClassName: NSObject <ProtocolName> 
  2. //...  
  3. @end 

另外,在调用特殊代理方法前,需要通过conformsToProtocol函数来判断代理对

象是否遵守定义的协议,如:

  1. if([mydelegate conformsToProtocol:@protocol(protocolName)])  
  2. {   
  3.       [mydelegate delegateMethod];   

或者使用

  1. respondsToSelector:@selector 

来判断代理对象是否实现相应的方法。

 

Objective-C协议和委托是本文呢要介绍的内容,主要介绍了Objective-C中协议和委托的方式,先来看详细内容。

 

 

 

AD:

Objective-C协议委托是本文呢要介绍的内容,主要介绍了Objective-C协议委托的方式,通过实例讲解让我们更快更方便的去学习Objective-C,先来看详细内容。

protocol-协议,就是使用了这个协议后就要按照这个协议来办事,协议要求实现的方法就一定要实现。

delegate-委托,顾名思义就是委托别人办事,就是当一件事情发生后,自己不处理,让别人来处理。

当一个A view 里面包含了B view

b view需要修改a view界面,那么这个时候就需要用到委托了。

需要几个步骤

1、首先定一个协议

2、a view实现协议中的方法

3、b view设置一个委托变量

4、把b view的委托变量设置成a view,意思就是 ,b view委托a view办事情。

5、事件发生后,用委托变量调用a view中的协议方法

例子:

  1. B_View.h:  
  2. @protocol UIBViewDelegate <NSObject> 
  3. @optional  
  4. - (void)ontouch:(UIScrollView *)scrollView; //声明协议方法  
  5. @end  
  6.  
  7. @interface BView : UIScrollView<UIScrollViewDelegate>   
  8. {  
  9.  id< UIBViewDelegate > _touchdelegate; //设置委托变量  
  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];  //调用协议委托  
  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; //设置委托  
  46.  [self.view addSubview: m_BView];  
  47. }  
  48.  
  49. - (void)ontouch:(UIScrollView *)scrollView  
  50. {  
  51.     //实现协议  
  52. }
posted @ 2013-01-24 16:22  六界剑仙  阅读(195)  评论(0)    收藏  举报