rvc

#import <UIKit/UIKit.h>
@class MyTextFileld;
@class MyButton;


@interface RootView : UIView







//声明属性
@property (nonatomic, retain) MyButton *button;
@property (nonatomic, retain) MyTextFileld *textField1;

@property (nonatomic, retain) UITextField *textField;


@end

#import "RootView.h"
#import "TouchView.h"
#import "MyButton.h"
#import "MyTextFileld.h"


@implementation RootView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        //初始化添加一个_textField
        _textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 200, 35)];
        _textField.borderStyle = UITextBorderStyleRoundedRect;
        _textField.placeholder = @"请输入密码";
        _textField.backgroundColor = [UIColor grayColor];
        [self addSubview:_textField];
        
        
        [self addAllView];
    }
    return self;
}


- (void)addAllView
{
    TouchView *touch1View  = [[TouchView alloc] initWithFrame:CGRectMake(100, 200, 100, 45)];
    touch1View.backgroundColor = [UIColor magentaColor];
    [self addSubview:touch1View];
    [touch1View release];
    
    //前面声明得有属性
    self.button = [[MyButton alloc]initWithFrame:CGRectMake(100, 250, 100, 35)];
    self.button.backgroundColor = [UIColor greenColor];
    
    [self addSubview:_button];
    NSLog(@"按钮");
   
    
    
    self.textField1 = [[MyTextFileld alloc] initWithFrame:CGRectMake(100,300, 100, 40)];
    _textField1.backgroundColor = [UIColor redColor];
    //_textField.hidden = NO;  //显示隐藏
    [self addSubview:_textField1];
    [_textField1 release];
    
}



- (void)dealloc
{
    [_button release];
    [_textField release];
    [_textField1 release];
    [super dealloc];
}

@end


#import <UIKit/UIKit.h>

@interface MyButton : UIView





//声明方法
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;



@end

#import "MyButton.h"


@interface MyButton ()
{
    id _target;
    SEL _action;
    UIControlEvents _controlEvents;
}

@end
@implementation MyButton


#pragma mark - 触发事件
- (void)addTarget:(id)target
           action:(SEL)action
 forControlEvents:(UIControlEvents)controlEvents
{
    
    
    //target:代表rootViewController对象
    //action:代表button的执行事件
    //forControlEvents触发事件UIControlEventTouchUpInside
    _target = target;
    _action = action;
    _controlEvents = controlEvents;
}






#pragma mark - 要写入在出发时间后面
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //当前对象执行事件
    
    //让target去执行
    if (_controlEvents == UIControlEventTouchUpInside) {
        
        //让_target去调用_action消息,然后把当前对象self作为参数
        [_target performSelector:_action withObject:self];
    }
}



@end

//告诉编译器,MyMyTextFieldDelegate是一个协议,类似@class
@protocol MyTextFieldDelegate;



@interface MyTextFileld : UIView
//添加delegate代理属性
@property (nonatomic, assign) id <MyTextFieldDelegate> delegate;

@end


#import "MyTextFileld.h"

@implementation MyTextFileld







- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //判断代理对象是否可移执行对象方法
    if ([_delegate respondsToSelector:@selector(textFieldDidBeginEditing:)]) {
        [_delegate textFieldBegan:self];
    }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    if ([_delegate respondsToSelector:@selector(textFieldEditing:)]) {
        
        [_delegate textFieldEditing:self];
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    if ([_delegate respondsToSelector:@selector(textFieldEnded:)]) {
        
        [_delegate textFieldEnded:self];
    }
}





@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值