一、收起键盘
在UIViewController中收起键盘,经常看到一些写法,如:
- (void)closeKeyBoard { if ([_userTextField isFirstResponder]) { [_userTextField resignFirstResponder]; } else if ([_pwdTextField isFirstResponder]) { [_pwdTextField resignFirstResponder]; } }
除了调用相应控件的resignFirstResponder方法外,还有另外三种方法:
1.重载UIViewController 中的 touchesBegan 方法,然后在里面执行[self.view endEditing:YES];
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesBegan:touches withEvent:event]; [self.view endEditing:YES]; }
2.当获得当前UIViewController比较困难的时候,直接执行
[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
3.直接执行
[[[UIApplication sharedApplication] keyWindow] endEditing:YES];
4.如果你的controller是一个tableView或者scrollview,可以通过添加手势的方式来处理
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardHide:)]; // 设置成NO表示当前控件响应后会传播到其他控件上,默认为YES。 tapGestureRecognizer.cancelsTouchesInView = NO; // 将触摸事件添加到当前view [_tableView addGestureRecognizer:tapGestureRecognizer];
- (void)keyboardHide:(UITapGestureRecognizer *)tap { [self.view endEditing:YES]; }
二、NSJSONSerialization比NSKeyedArchiver更好
在选择持久化方案时,系统提供NSJSONSerialization比NSKeyedArchiver在效率和体积上都更优。经过简单测试,NSJSONSerialization比NSKeyedArchiver快了7倍,而且序列化之后的体积是NSKeyedArchiver的一半。
网上也有更详细的测试证明了该说法:https://github.com/randomsequence/nsserialisationtests
未完会持续补充~~~~