Target—Action(目标—动作)
控件通过 目标—动作的方式通知应用程序
用户和视图进行交互的时候,控件将消息(动作)发送给对象(目标),对象做出相应的处理。
Target—Action主要用来在MVC设计模式中,V和C之间通信。V只负责发送对应的action给target,不关心target具体做了什么。降低模块之间的耦合性。
1、 target - action,目标动作机制
创建一个继承于UIViewController的RootViewController,并设置为根视图控制器,创建一个继承于UIView的TouchView:
首先在TouchView.h定义一个关联事件的方法
-(void)addTarget:(id)targetaction:(SEL)action;
在TouchView.m里边实现:
-(void)addTarget:(id)targetaction:(SEL)action{
NSLog(@"%@", target);
NSLog(@"%@",NSStringFromSelector(action));
}
要想target action在touchesEnded中使用,需要定义两个实例变量 在TouchView.h
@interfaceTouchView : UIView{
//定义两个实例变量,存方法的执行者 和 方法名
id _target;
SEL _action;
}
然后在TouchView.m初始化
_target = target;
_action = action;
然后可以在(Began、Moved、Ended或者Cancelled)中写:[_target performSelector:_action withObject:self];
2、 delegate,代理模式
完整代码如下:
// RootViewController.m
#import "RootViewController.h"
#import "TouchView.h"
#import "TouchViewPro.h"
@interface RootViewController ()<TouchViewProDelegate>{
TouchView *touchView;
TouchViewPro *touchViewProw;
}
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor yellowColor];
touchView = [[TouchView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
touchView.backgroundColor = [UIColor greenColor];
[self.view addSubview:touchView];
[touchView release];
//View - Controller 的通信模式
//1、 target - action, 目标--动作机制
/*
[touchView addTarget:self action:@selector(touch:)];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(225, 100, 100, 100);
[button setTitle:@"登录" forState:UIControlStateNormal];
[button addTarget:self action:@selector(pressButton:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
*/
/*
//2、 delegate,代理模式
touchViewProw = [[TouchViewPro alloc]initWithFrame:CGRectMake(100, 300, 200, 200)];
touchViewProw.backgroundColor = [UIColor whiteColor];
touchViewProw.delegate = self;
[self.view addSubview:touchViewProw];
[touchViewProw release];
*/
}
-(void)pressButton:(UIButton *)button{
NSLog(@"%@", button);
}
-(void)touch:(TouchView *)aTouchView{
NSLog(@"%@", aTouchView);
//改变颜色
// touchView.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255. green:arc4random() % 256 / 255. blue:arc4random() % 256 / 255. alpha:1.0];
//改变大小
touchView.bounds = CGRectMake(0, 0, arc4random() % 375, arc4random() % 667);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark - TouchViewProDelegate
-(void)touchesBegan:(NSSet *)touches{
NSLog(@"开始");
}
-(void)touchesMoved:(NSSet *)touches{
NSLog(@"移动");
}
-(void)touchesEnded:(NSSet *)touches{
NSLog(@"结束");
touchViewProw.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255. green:arc4random() % 256 / 255. blue:arc4random() % 256 / 255. alpha:1.0];
}
-(void)touchesCancelled:(NSSet *)touches{
NSLog(@"取消");
}
@end
// TouchView.h
#import <UIKit/UIKit.h>
@interface TouchView : UIView{
//定义两个实例变量,存方法的执行者 和 方法名
id _target;
SEL _action;
}
//1、定义一个关联事件的方法
-(void)addTarget:(id)target action:(SEL)action;
@end
// TouchView.m
#import "TouchView.h"
@implementation TouchView
-(void)addTarget:(id)target action:(SEL)action{
NSLog(@"%@", target);
NSLog(@"%@", NSStringFromSelector(action));
//target = rootViewController
_target = target;
//action = touch
_action = action;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
// self.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255. green:arc4random() % 256 / 255. blue:arc4random() % 256 / 255. alpha:1.0];
//让_target执行方法_action
//_target = rootViewContorller
//_action = touch
//_target,方法执行者
//参数1 : 方法名
//参数2 : 方法的参数
[_target performSelector:_action withObject:self];
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
}
@end
// TouchViewPro.h
#import <UIKit/UIKit.h>
@protocol TouchViewProDelegate <NSObject>
-(void)touchesBegan:(NSSet *)touches;
-(void)touchesMoved:(NSSet *)touches;
-(void)touchesEnded:(NSSet *)touches;
-(void)touchesCancelled:(NSSet *)touches;
@end
@interface TouchViewPro : UIView
@property (nonatomic, assign) id<TouchViewProDelegate>delegate;
@end
// TouchViewPro.m
#import "TouchViewPro.h"
@implementation TouchViewPro
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
if ([_delegate respondsToSelector:@selector(touchesBegan:)]) {
[_delegate touchesBegan:touches];
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
if ([_delegate respondsToSelector:@selector(touchesMoved:)]) {
[_delegate touchesEnded:touches];
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
if ([_delegate respondsToSelector:@selector(touchesEnded:)]) {
[_delegate touchesEnded:touches];
}
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
if ([_delegate respondsToSelector:@selector(touchesCancelled:)]) {
[_delegate touchesCancelled:touches];
}
}
@end
// TouchViewProDelegate.h
#import <Foundation/Foundation.h>
/*
可以把内容拷贝到TouchViewPro.h里边,比较简单
@protocol TouchViewProDelegate <NSObject>
-(void)touchesBegan:(NSSet *)touches;
-(void)touchesMoved:(NSSet *)touches;
-(void)touchesEnded:(NSSet *)touches;
-(void)touchesCancelled:(NSSet *)touches;
@end
*/