rvc
2015-06-05 23:07
501 查看
#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
相关文章推荐
- Android中Menu的基本用法 .
- 【c++】堆排序算法
- 多线程面试秒杀系列6--临界区函数的使用
- [D3] 14. Line and Area Charts with D3
- 第二十篇 人生中的突破口
- Uva 11584 Partitioning by Palindromes(区间dp)
- 2015-06-05
- 多项式相加
- WriteableBitmap/BitmapImage/MemoryStream/byte[]相互转换
- OpenCSP开源程序解析之OPENCSP_Hash.cpp
- JAVA 大数在acm中的应用
- Base64 Base32 Base16全家桶
- codeforces C. Divisibility by Eight (纯属无聊来发一篇博客)
- ct任务添加与中控机批量后台操作
- hdu 5256 序列变换 (LIS变形)
- python虚拟环境的使用
- 操作系统--存储管理的任务
- 操作系统--存储管理的任务
- 初探字符集和字符集编码
- Python函数式编程实践