target --action设计模式,以及delegate设计模式

一.target --action设计模式
1.创建视图控制器,在视图控制器.m文件中,创建CustomView (UIView的子类)的对象
CustomView *blueView = [[CustomView alloc] initWithFrame:CGRectMake(20, 180, 280, 100)];
    blueView.backgroundColor = [UIColor blueColor];
    [blueView addTarget:self action:@selector(changeLocat:)forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:blueView];
    [blueView release];

2.在CustomView.h文件中定义方法
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;(这个方法和UIButton类中相似)
3.在CustomView.m文件中,
@interface CustomView ()
{
    id _target;
    SEL _action;
    UIControlEvents _controlEvents;
}
@end
@implementation CustomView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    当当前视图接收到触摸事件之后,交由target去处理
    if ( UIControlEventTouchDown==_controlEvents) {
          [_target performSelector:_action withObject:self];
    }
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (UIControlEventTouchUpInside ==_controlEvents) {
          [_target performSelector:_action withObject:self];
    }
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

}
为当前视图指定当前视图接收到响应事件之后,有target来通过action方法进行响应
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
{
    //用实例变量存储外界传入的参数,方便在其他方法中使用
    _target = target;
    _action = action;
    _controlEvents = controlEvents;
}



4.在视图控制器的.m文件中,实现调用的方法
- (void)changeSupColor:(CustomView *)view
{
    view.superview.backgroundColor = [UIColor random];
}

二.delegate设计模式
1.代理设计模式的思想:对于当前视图对象,只负责接收触摸事件,当触摸事件发生之后,通知代理做相应处理,代理如何处理,视图不关心.
2.创建视图控制,在其.m文件中,定义TouchView(UIView的子类)类的对象
TouchView *redView = [[TouchView alloc] initWithFrame:CGRectMake(20, 100, 280, 100)];
    redView.backgroundColor = [UIColor redColor];
    [self.view addSubview:redView];
    redView.tag =100;
    //为视图指定代理
    redView.delegate = self;
    [redView release];

3.在TouchView .h文件,定义协议以及方法和属性 
@class TouchView;
@protocol TouchViewDelegate <NSObject>
//协议中存在四个方法,对应触摸四个时机 ,当视图对象接收到不同的触摸事件之后,通过协议中的方法通知代理对象.
@optional
- (void)touchViewTouchesBegan:(TouchView *)touchView;
- (void)touchViewTouchesMoved:(TouchView *)touchView;
- (void)touchViewTouchesEnded:(TouchView *)touchView;
- (void)touchViewTouchesCancelled:(TouchView *)touchView;
@end
@interface TouchView : UIView
@property (nonatomic , assign) id<TouchViewDelegate> delegate;
@end

4.在TouchView .m文件
代理设计模式的思想:对于当前视图对象,只负责接收触摸事件,当触摸事件发生之后,通知代理做相应处理,代理如何处理,视图不关心.
(1).- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //原理:
    //如果代理实现了对应的协议中的方法,就让代理调用方法,如果没有实现,就不要调用了
    //判断代理是否实现了对应的方法,(判断一个对象是否实现了某个特定的方)
    if ([self.delegate respondsToSelector:@selector(touchViewTouchesBegan:)]) {
        //触摸开始时,通知代理做相应操作
        [self.delegate touchViewTouchesBegan:self];
    }
    
}
(2).- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    if ([self.delegate respondsToSelector:@selector(touchViewTouchesEnded:)]) {
        //触摸结束时,通知代理做相应操作
        [self.delegate touchViewTouchesEnded:self];
    }    
}
(3).-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    if ([self.delegate respondsToSelector:@selector(touchViewTouchesMoved:)]) {
        //触摸移动时,通知代理做相应操作
        [self.delegate touchViewTouchesMoved:self];
    } 
}

(4).- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    if ([self.delegate respondsToSelector:@selector(touchViewTouchesCancelled:)]) {
        //触摸取消时,通知代理做相应操作
        [self.delegate touchViewTouchesCancelled:self];
    }   
}

5.在视图控制器的.m文件中,实现方法.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值