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;

}




④将AppDelegateLOButton对象的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];

    }



0 0
原创粉丝点击