iOS开发 delegate,SEL和Block

来源:互联网 发布:获取端口号 编辑:程序博客网 时间:2024/05/15 02:06

iOS开发 delegate,SEL和Block


在Object-c中解决子类和父类之间事件传递,不同根类之间事件传递的三种方法
由于Object-c是单向继承的,父类 向子类传事件,数据,可用点语法,或者直接调用成员函数即可,
但是相反方向则需要引入委托,回调这样的概念
下面我将给出继承UIview,为他的子类添加点击事件,让他去改变父视图的背景色这个简单的例子的三种实现方法:


首先建立一个MyView 继承UIView

1.Delegate    编写协议

头文件  MyView.h

//

//  MyView.h

//  myView

//

//  Created by mac_shun on 13-9-7.

//  Copyright (c) 2013 wangshunPush. All rights reserved.

//


#import <UIKit/UIKit.h>


@protocol MyViewDelegate;


@interface MyView :UIView

{

   id <MyViewDelegate> _delegate;

}

@property (nonatomic,retainid <MyViewDelegate> delegate;


@end



@protocol MyViewDelegate <NSObject>


-(void)changeColor;


@end


协议中给出changeColor方法,让遵守该协议的类来实现这个方法
实现文件   MyView.m

//

//  MyView.m

//  myView

//

//  Created by mac_shun on 13-9-7.

//  Copyright (c) 2013 wangshunPush. All rights reserved.

//


#import "MyView.h"


@implementation MyView

@synthesize delegate =_delegate;


- (id)initWithFrame:(CGRect)frame

{

   self = [superinitWithFrame:frame];

   if (self) {

        // Initialization code

        

       //允许触发

        self.userInteractionEnabled =YES;

    }

    return self;

}


-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

{

   if (self.delegate &&  [self.delegate respondsToSelector:@selector(changeColor)]) {

        //  delegate 去掉 自己实现的  changeColor 方法

        [self.delegate performSelector:@selector(changeColor)];

    }

}



/*

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect

{

    // Drawing code

}

*/


@end



头文件 ViewController.h
在控制器中实例化MyView

#import <UIKit/UIKit.h>

#import "MyView.h"


@interface ViewController :UIViewController<MyViewDelegate>


@end


下面是实现文件 ViewController.m

//

//  ViewController.m

//  myView

//

//  Created by mac_shun on 13-9-7.

//  Copyright (c) 2013 wangshunPush. All rights reserved.

//


#import "ViewController.h"


@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad

{

    [superviewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

    self.view.backgroundColor =[UIColorscrollViewTexturedBackgroundColor];

    

    MyView *v =[[MyViewalloc]initWithFrame:CGRectMake(0,0,100,100)];

    v.backgroundColor = [UIColorredColor];

    v.delegate =self;

    

    [self.viewaddSubview:v];

    

}


-(void)changeColor//协议方法

{

    self.view.backgroundColor = [UIColorunderPageBackgroundColor];

}


- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


2.SEL    

还是Myview  加入SEL 变量
并加入一个addTarget方法

头文件  MyView.h

#import <UIKit/UIKit.h>




@interface MyView :UIView

{

   id  delegate;

    

   SEL method;

}


-(void)addTarget:(id) d action:(SEL) m;


@end


实现文件   MyView.m

//

//  MyView.m

//  myView

//

//  Created by mac_shun on 13-9-7.

//  Copyright (c) 2013 wangshunPush. All rights reserved.

//


#import "MyView.h"


@implementation MyView


- (id)initWithFrame:(CGRect)frame

{

   self = [superinitWithFrame:frame];

   if (self) {

        // Initialization code

        

       //允许触发

        self.userInteractionEnabled =YES;


    }

    return self;

}


-(void)addTarget:(id)d action:(SEL)m

{

   delegate = d;

    

   method   = m;

    

}


-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

{

    if (delegate && [delegaterespondsToSelector:method]) {

        //delegate  method 方法

        [delegateperformSelector:methodwithObject:self];

    }

    

}



/*

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect

{

    // Drawing code

}

*/


@end



下面ViewController.m

#import "ViewController.h"


@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad

{

    [superviewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

    self.view.backgroundColor =[UIColorscrollViewTexturedBackgroundColor];

    

    MyView *v =[[MyViewalloc]initWithFrame:CGRectMake(0,0,100,100)];

    v.backgroundColor = [UIColorredColor];

    

    [vaddTarget:selfaction:@selector(changeColorForSEL:)];

    

    [self.viewaddSubview:v];

    

}


-(void)changeColorForSEL:(MyView*)view

{

    self.view.backgroundColor = [UIColorunderPageBackgroundColor];

}


3.Block方法

函数指针

头文件  MyView.h

#import <UIKit/UIKit.h>


@interface MyView :UIView

{

    //定义一个指针变量 getMethod,这个指针指向一个函数

   void  (^getMethod)(MyView*);

    

}

/*

    MyView添加一个方法  

    

    形参      m

 

    形参类型  为一个函数的地址 void (^)(MyView*)

*/

-(void)addBlockMethod:(void (^)(MyView* view)) m;


@end



实现文件   MyView.m

#import "MyView.h"


@implementation MyView


- (id)initWithFrame:(CGRect)frame

{

   self = [superinitWithFrame:frame];

   if (self) {

        // Initialization code

        

       //允许触发

        self.userInteractionEnabled =YES;


    }

    return self;

}


-(void)addBlockMethod:(void (^)(MyView * view))m

{

    //获得外部函数的地址

    [getMethod release];

    

   getMethod = [mcopy];

    

}


-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

{

    if (getMethod) {//当触发view调用已经指向的函数

       getMethod(self);

    }

}



@end



头文件   ViewController.h

#import <UIKit/UIKit.h>

#import "MyView.h"


@interface ViewController :UIViewController


@end


实现文件   ViewController.m

#import "ViewController.h"


@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad

{

    [superviewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

    self.view.backgroundColor =[UIColorscrollViewTexturedBackgroundColor];

    

    MyView *v =[[MyViewalloc]initWithFrame:CGRectMake(0,0,100,100)];

    v.backgroundColor = [UIColorredColor];

    

    [vaddBlockMethod:^(MyView *view) {

      self.view.backgroundColor =[UIColorunderPageBackgroundColor];

     }];

    

    [self.viewaddSubview:v];

    

}


- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


0 0
原创粉丝点击