您的位置:首页 > 产品设计 > UI/UE

UIButtion解耦

2015-09-02 20:10 281 查看
1.target/action设计模式

适用于控件,不关心哪些方法 tagert/action不需要对象给它传值

目标-行为(Target-Action)模式(目的在于让代码解耦合,使代码与代码之间关联性降低,便于后期开发维护)

Target-action----这个设计模式用按钮,等控件把用户的交互变成代码,让程序可以执行;
Target-action :通俗易懂的说也就是

一个对象包含一些生成一个消息表达式的元素,当一个确定事件出现时,把这些元素放到一起组成消息和发送这个消息。

2.代理设计模式

当一个类的某些功能需要被别人来实现,但是既不明确是些什么功能,又不明确谁来实现这些功能的时候,委托模式就可以派上用场。

可以互相传值

①②③④⑤⑥⑦⑧⑨⑩

1.target/action
①在LOButton.h中设置

typedef enum {
LOButtonEventDown,
LOButtonEventTouchUpInside
}LOButtonEvent;
//按钮的事件类型

@interface LOButton : UIView
@property (nonatomic,
retain) UILabel *titleLable;
//按钮的标lable

//通过tagegt/action设计模式为按钮绑定对应的响应方法
- (void)addTarget:(id)target action:(SEL)action forButtonEven:(LOButtonEvent)event;

②在LOButton.m中声明私有属性
@interface LOButton ()

@property (nonatomic,
assign) id target;

@property (nonatomic,
assign) SEL action;
@property (nonatomic,
assign) LOButtonEvent event;

@end

③titleLable的懒加载,如果不再AppDelegate中加载,就不会出现在button上
-(void)dealloc{
[_titleLable release];
[super dealloc];
}
- (UILabel *)titleLable{

if (!_titleLable) {

self.titleLable = [[[UILabel alloc] initWithFrame:self.bounds] autorelease];
_titleLable.textAlignment = NSTextAlignmentCenter;

//添加在BUTTON上
[self addSubview:_titleLable];
}

return _titleLable;
}

④将AppDelegate中LOButton对象的target,action,event参数传递给此方法
- (void)addTarget:(id)target action:(SEL)action forButtonEven:(LOButtonEvent)event{

self.target = target;

self.action = action;

self.event = event;
}

⑤根据传过来的参数执行方法
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

if (self.event == LOButtonEventDown) {

//withObject后跟参数,及self的所有参数
[self.target performSelector:self.action withObject:self];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

if (self.event == LOButtonEventTouchUpInside) {
[self.target performSelector:self.action withObject:self];
}

⑥AppDelegate.m中
加载函数中创建LOButton类的对象,给addTarget:action:forButtonEven:方法传参数
LOButton *button = [[LOButton alloc] initWithFrame:CGRectMake(50,
50, 70,
20)];
button.titleLable.text =
@"按扭";
button.backgroundColor = [UIColor yellowColor];
[button addTarget:self action:@selector(changeBackgrond:) forButtonEven:LOButtonEventDown];
[button addTarget:self action:@selector(changeBackgrond:) forButtonEven:LOButtonEventTouchUpInside];
[self.window addSubview:button];

⑦AppDelegate.m中实现changeBackgrond:方法
- (void)changeBackgrond:(UIButton *)sender{

self.window.backgroundColor = [UIColor colorWithRed:arc4random() %
256 / 255.0 green:1 blue:1 alpha:1];
}

2.代理设计模式

①②③④⑤⑥⑦⑧⑨⑩
①在LOButton.h中
@class LOButton;
//提前声明有此类存在

②在LOButton.h中
定名字叫LOButtonDelegate的协议(可选择)
@protocol LOButtonDelegate <NSObject>

@optional
- (void)buttonDidClickedDown:(LOButton *)button;//按钮已经按下
- (void)buttonDidClickedUpInside:(LOButton *)button;
//按钮弹起操作

@end

③LOButton.h中
在委托方声明一个id属性,来决定谁是代理
@interface LOButton : UIView
@property (nonatomic,
retain) UILabel *titleLable;
@property (nonatomic,
assign) id<LOButtonDelegate> delegate;

@end

④在LOButtion.m中 titleLable的懒加载
-(void)dealloc{
[_titleLable release];
[super dealloc];
}

- (UILabel *)titleLable{

if (!_titleLable) {

self.titleLable = [[[UILabel alloc] initWithFrame:self.bounds] autorelease];
_titleLable.textAlignment = NSTextAlignmentCenter;
[self addSubview:_titleLable];
}

return _titleLable;
}

⑤在LOButton.m中
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

//如果协议是@optional
需要判断是否有代理并且代理对方法是否做出响应

if (self.delegate && [self.delegate respondsToSelector:@selector(buttonDidClickedDown:)]) {
[self.delegate buttonDidClickedDown:self];}
}

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

if (self.delegate && [self.delegate respondsToSelector:@selector(buttonDidClickedUpInside:)]) {
[self.delegate buttonDidClickedUpInside:self];}
}

⑥AppDelegate.m中
要遵守的协议,及可选择要实现的方法(此协议中的方法为@optional的)
@interface AppDelegate ()<LOButtonDelegate>

@end

⑦AppDelegate.m中
加载函数中创建LOButton类的对象,指定代理
LOButton *button = [[LOButton alloc] initWithFrame:CGRectMake(50,
50, 70,
20)];
button.delegate =
self;
[self.window addSubview:button];
[button release];

⑧AppDelegate.m中
实现buttonDidClickedUpInside:方法,则touchesEnded:touches withEvent:方法检测到有此方法做出响应
- (void)buttonDidClickedUpInside:(LOButton *)button{

self.window.backgroundColor = [UIColor colorWithRed:arc4random() %
256 / 255.0 green:1 blue:1 alpha:1];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: