target/action 设计模式简单使用

Target-Action模式是ObjC里非常常见的对象之间方法调用的方式,不过ObjC把方法调用叫做Send
Message.

一帮情况在和UI打交道时候处理各种GUI上的事件会利用到这种模式.相对应的.NET上的处理模式就是delegate/event了.
不过,Target-Action拜C语言所赐,更是灵活很多,编译期没有任何检查,都是运行时的绑定.

代码演示:

1.创建一个继承UIView的类

#import <UIKit/UIKit.h>

@interface TouchView : UIView
//模拟的Button的写法

//实现方法的独享
@property (nonatomic,assign) id target ;

//实现的方法
@property (nonatomic,assign) SEL action ;

@end

实现部分:

@implementation TouchView

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    //self.target 实现self.action的方法
    //withObject:一般用来实现多线程之间的通信
    [self.target performSelector:self.action withObject:self];

}

@end

初始化:

#import "RootViewController.h"
#import "TouchView.h"
@interface RootViewController ()

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];

    // target / action
    /*
     面向对象编程的核心思想:高内聚,低耦合
     target/action:可以用来解耦
     */

    TouchView *touchView1 = [[TouchView alloc]initWithFrame:CGRectMake(100, 100, 175, 100)];
    touchView1.backgroundColor = [UIColor magentaColor];

    TouchView*touchView2 = [[TouchView alloc]initWithFrame:CGRectMake(100, 250, 175, 100)];
    touchView2.backgroundColor = [UIColor redColor];

    //对target赋值
    touchView1.target = self ;

    //指定要实现的方法,对action赋值
    touchView1.action = @selector(touchAction1:);
    touchView1.tag = 111 ;
    [self.view addSubview:touchView1];
    [touchView1 release];

    touchView2.target = self ;
    touchView2.action [email protected](touchAction2:) ;
    touchView2.tag = 222 ;
    [self.view addSubview:touchView2];
    [touchView2 release];

}

-(void) touchAction1:(TouchView *)touchView
{
    TouchView *tempTouchView = (TouchView *)[self.view viewWithTag:111];
    tempTouchView.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:arc4random()%256/255.0];
}

-(void) touchAction2:(TouchView *)touchView{
    TouchView *tempTouchView1 = (TouchView *)[self.view viewWithTag:222];
//    tempTouchView1.center = CGPointMake(arc4random() %180 +90, arc4random() %567 + 50);
    tempTouchView1.frame = CGRectMake(arc4random() %201, arc4random() % 568, 175, 100);

}

@end
时间: 2024-08-24 02:40:57

target/action 设计模式简单使用的相关文章

UI开发----target/action设计模式和代理设计模式以及手势识别器

//  Created By 郭仔  2015年04月16日21:50:33 眼睛有点痛,视力也在急速下降,心灵之窗,注意保护! ================================================ 耦合是衡量?一个程序写的好坏的标准之?一, 耦合是衡量模块与模块之间关联程度的指标 "?高内聚,低耦合"是?面向对象编程的核⼼心思想: ================ 设计模式思想很重要的 ================== target/action设计模

target - action设计模式的思想

不同的实例点击效果不同:点击改变自身颜色,点击改变父视图颜色,点击修改视图位置.以上效果可由target - action设计模式实现. - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor yellowColor]; CustomView *greenView = [[CustomView

UI基础:target...action设计模式,手势识别器.UIimageview

使用target..action和delegate设计模式可以实现解耦.使代码更加优化. 手势识别器: 手势识别器:是对触摸事件做了封装,无需自己去判断某个手势是否触发,手势识别器本身起到了识别作用,所在我们可以把重心放在识别之后该去做什么操作上面.很方便. 手势识别器是iOS中比较抽象的一个类,用于识别一个手势,所谓的手势:有规律的触摸. 手势识别器有7个子类: 分别是:轻怕手势,轻移手势,清扫手势,缩放手势,旋转手势,长按手势,以及屏幕边缘平移手势. 一旦指定的手势别识别了,就可以执行自定义

UI 04 target...action 设计模式

"高内聚,低耦合"是面向对象编程的核心思想. 使用 target-action 实现解耦. 需要目标去执行一个动作的地方. 例如, 定义一个继承于UIView 的MyButton 类, 让他能够有Button的点击方法. 代码如下: Mybutton.h: #import <UIKit/UIKit.h> @interface Mybutton : UIView //1.写一个自定义方法,把目标和对应动作传过来. - (void)addNewTarget:(id)target

target/action 设计模式

Target-Action模式是ObjC里非常常见的对象之间方法调用的方式,不过ObjC把方法调用叫做Send Message. 一帮情况在和UI打交道时候处理各种GUI上的事件会利用到这种模式.相对应的.NET上的处理模式就是delegate/event了. 不过,Target-Action拜C语言所赐,更是灵活很多,编译期没有任何检查,都是运行时的绑定. 看代码 首先我们创建一个继承UIView的类 1 #import <UIKit/UIKit.h> 2 3 @interface Touc

target-action设计模式--主要为Button的方法重写

新建两个类MainViewController/ButtonView ButtonView.h #import <UIKit/UIKit.h> @interface ButtonView : UIView //实现target-action设计模式 //点击的时候让谁去执行方法 @property (nonatomic , assign) id target; //要执行的方法 @property (nonatomic , assign) SEL action; //模拟一个UIButton的

UI-target...action设计模式,手势识别器.UIimageview

target-action设计模式 iOS设计模式之Target-Action主要是为了降低代码的耦合性.顾名思义      Target-Action模式就是指  目标-动作模式,它贯穿于iOS开发始终. 提到Target-Action,先说2个词     “高内聚,低耦合”      这主要是评价一个软件的好坏 它评判软件的好坏主要靠模板之间内聚是否高,模块间耦合度是否低. 其实Target-action模式很简单,就是当某个事件发生时,调用那个对象中的那个方法.比如:点击按钮时,调用Con

进击的UI---------------target/action设计模式&amp;Delegate&amp;手势识别

1.target/action设计模式: AppDelegate.m RootViewController.m ClickView.h ClickView.m ColorView.h ColorView.m RootView.h RootView.m ButtonView.h ButtonView.m 2.delegate设计模式(代理): AppDelegate.m RootViewController.m RootView.h: RootView.m: colorView.h colorVi

Target Action And UIControl

Target Action And UIControl target-action的设计模式 UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; button.frame = CGRectMake(135, 100, 80, 40); [button setTitle:@"??" forState:UIControlStateNormal]; button.backgroundColor = [UIColor