assign和weak的区别
- 本质区别
- 速度比较 : __unsafe_unretained > __weak
@property (nonatomic, assign) UIView *view; // UIView *__unsafe_unretained _view;
__unsafe_unretained的特点:
1. 不是强引用, 不能保住OC对象的命
2. 如果引用的OC对象销毁了, 指针并不会被清空
依然指向销毁的对象(很容易产生野指针错误: EXC_BAD_ACCESS)
@property (nonatomic, weak) UIView *view; // UIView *_Nullable _-weak _view;
__weak的特点
1. 不是强引用, 不能保住OC对象的命
2. 如果引用的OC对象销毁了, 指针会被自动清空(变为nil)
不再指向销毁的对象(永远不会产生野指针错误)
- 用途
- assign一般用在基本数据类型上面, 比如int\double等
- weak一般用在代理对象上面, 或者用来解决循环强引用的问题
监听UITextField的获得焦点和失去焦点的事件
- addTarget
UIControlEventEditingDidBegin:
1.开始编辑
2.获得焦点
3.弹出键盘
[textField addTarget:target action:@selector(editingDidBegin) forControlEvents:UIControlEventEditingDidBegin];
/**********************/
UIControlEventEditingDidEnd:
1.结束编辑
2.失去焦点
3.退下键盘
[textField addTarget:target action:@selector(editingDidEnd) forControlEvents:UIControlEventEditingDidEnd];
/**********************/
UIControlEventEditingChanged:
1.正在编辑
2.内容改变
3.键盘输入
[textField addTarget:target action:@selector(editingChange) forControlEvents:UIControlEventEditingChanged];
- delegate
textField.delegate = self;
#pragma mark - <UITextFieldDelegate>
- (void)textFieldDidBeginEditing:(UITextField *)textField {
[self setValue:[UIColor whiteColor] forKeyPath:DCPlaceholderColorKey];
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
[self setValue:[UIColor grayColor] forKeyPath:DCPlaceholderColorKey];
}
- 通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(beginEditing) name:UITextFieldTextDidBeginEditingNotification object:textField];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endEditing) name:UITextFieldTextDidEndEditingNotification object:textField];
#pragma mark - TextFieldNotification
- (void)beginEditing {
[self setValue:[UIColor whiteColor] forKeyPath:DCPlaceholderColorKey];
}
- (void)endEditing {
[self setValue:[UIColor grayColor] forKeyPath:DCPlaceholderColorKey];
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- 通知2:使用block监听通知
//object对象发出了名字为name的通知, 就在queue队列中执行block
self.observer = [[NSNotificationCenter defaultCenter] addObserverForName:UITextFieldTextDidBeginEditingNotification object:self queue:[[NSOperationQueue alloc] init] usingBlock:^(NSNotification * _Nonnull note) {
//一旦监听到通知, 就会执行这个block中的代码
}];
// 最后需要移除监听
[[NSNotificationCenter defaultCenter] removeObserver:self.observer];
- 使用内部的一些机制
- 比如利用UITextFiled的
becomeFirstResponder
和resignFirstResponder
来监听UITextFiled的获得焦点和失去焦点事件
- 比如利用UITextFiled的
/**
* 调用时刻 : 成为第一响应者(开始编辑/弹出键盘/获得焦点)
*/
- (BOOL)becomeFirstResponder {
[self setValue:[UIColor whiteColor] forKeyPath:DCPlaceholderColorKey];
return [super becomeFirstResponder];
}
/**
* 调用时刻 : 放弃第一响应者(结束编辑/退出键盘/失去焦点)
*/
- (BOOL)resignFirstResponder {
[self setValue:[UIColor grayColor] forKeyPath:DCPlaceholderColorKey];
return [super resignFirstResponder];
}
枚举值的某个规律
- 凡是使用了1 << n格式的枚举值, 都可以使用 | 或运算符 进行组合使用
UIControlEventEditingDidBegin = 1 << 16,
UIControlEventEditingChanged = 1 << 17,
UIControlEventEditingDidEnd = 1 << 18,
UIControlEventEditingDidEndOnExit = 1 << 19,
// 使用情况
[self addTarget:self action:@selector(test) forControlEvents:UIControlEventEditingDidBegin | UIControlEventEditingChanged];