#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