target --action设计模式,以及delegate设计模式
2014-08-26 20:13
302 查看
一.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文件中,实现方法.
相关文章推荐
- UI笔记_target...action设计模式
- Target_Action 设计模式
- target-action设计模式
- UI target/action设计模式
- Target/Action 设计模式
- target-action设计模式--主要为Button的方法重写
- Target- Action设计模式
- UI基础:target...action设计模式,手势识别器.UIimageview
- iOS通讯模式(KVO、Notification、Delegate、Block、Target-Action的区别)
- Target-Action 设计模式
- iOS 对于Target-Action设计模式的理解
- target/action 设计模式
- UI - Target-Action设计模式
- iOS通讯模式(KVO、Notification、Delegate、Block、Target-Action的区别)
- UI:target-action设计模式、手势识别器
- ios开发中的基本设计模式(代理,观察者,MVC,单例,策略,工厂,MVVM,原型,Target-Action,通知(notification)机制)
- target - action 设计模式
- iOS如何实现传值---target-action设计模式
- iOS设计模式之Target-Action
- UI开发----target/action设计模式和代理设计模式以及手势识别器