UIButtion解耦
来源:互联网 发布:中山淘宝电商培训机构 编辑:程序博客网 时间:2024/06/05 15:29
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];
}
- UIButtion解耦
- 解耦
- 解耦
- 解耦
- 趣谈“解耦”
- 解耦-大宽表
- Mvp解耦
- [草稿]VSP解耦
- 感悟--消冗,解耦
- struts2与Servlet解耦
- java_jdbc_3层 解耦
- Struts2与Servlet解耦
- 架构,设计,解耦
- 三层架构问题----解耦
- 使用trigger解耦
- iOS TableView解耦
- 编程思想-解耦
- 与Servlet API解耦
- 线程中join的理解
- Objective-C之类的声明与创建
- XML 和 JSON 解析数据
- POJ 3320 Jessica's Reading Problem(尺取法)
- Android内存优化(五)-- 分析内存的使用
- UIButtion解耦
- iOS开发中的UI所有知识点
- vector interator map set(multiset)
- 快排
- TCP/IP中的CLOSE_WAIT和TIME_WAIT
- Google 发布 Android 性能优化典范
- Git常用命令
- CSS伪类和伪元素
- EditText的几种inputType