码迷,mamicode.com
首页 > 其他好文 > 详细

代理的使用

时间:2015-04-11 20:45:43      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:

MyView.h

 @protocal ViewTouchDelegate<NSObject>

  @required

  -(void)touchMyView;

 @end

 

{

  id<ViewTouchDelegate>delegate;

}

@property (nonamtic,assign)id<ViewTouchDelegate>delegate;

 

-(void)touchMyView;

 

 

MyView.m

-(id)init

{

  self =[super init];

  if(self)

       {

      [self touchMyView];

       }     

  return self;

}

 

-(void)touchMyView

{

  delegate =_delegate;

 

  if([_delegate conformsToProtocal:@protocal(ViewTouchDelegate)])

  {

    if([_delegate respondsToSelector:@selector(touchMyView)])

    {

      [_delegate touchMyView];

            }

       }

}

 

 

MyViewController.h

#import "MyView.h"

@interface MyViewController:UIViewController <ViewTouchDelegate>

 

MyViewController.m

-(void)viewDidLoad

{

  [super viewDidLoad];

  MyView *_myView =[[MyView alloc]init];

  _myView.frame =CGRectMake(0,0,100,100);

  _myView.delegate=self;

  [self.view addSubView:_myView];

}

 

-(void)touchMyView

{

  NSLog(@"Delegate is doing things");

}

 

代理的使用

标签:

原文地址:http://www.cnblogs.com/520myp1314/p/4418262.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!