ios键盘(搜索按钮)
//键盘右下角
//枚举
typedef NS_ENUM(NSInteger, UIReturnKeyType) {
UIReturnKeyDefault, //默认:灰色按钮,标有Return
UIReturnKeyGo, //标有Go的蓝色按钮
UIReturnKeyGoogle, //标有Google的蓝色按钮,用于搜索
UIReturnKeyJoin, //标有Join的蓝色按钮
UIReturnKeyNext, //标有Next的蓝色按钮
UIReturnKeyRoute, //标有Route的蓝色按钮
UIReturnKeySearch, //标有Search的蓝色按钮
UIReturnKeySend, //标有Send的蓝色按钮
UIReturnKeyYahoo, //标有Yahoo!的蓝色按钮,用于搜索
UIReturnKeyDone, //标有Done的蓝色按钮
UIReturnKeyEmergencyCall, //紧急呼叫按钮
UIReturnKeyContinue NS_ENUM_AVAILABLE_IOS(9_0),
};
//用法用例:
xxx.returnKeyType = UIReturnKeyGo;
这个时候你发现按钮不管用
那么我们该看看代理到底有什么方法
//UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField;
类似这个方法 就可以加处理了
————————————————
版权声明:本文为CSDN博主「巴糖0547」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/heberan/article/details/52215417
UITextField 监听数值变化的四种方法
-(void)addTargetMethod{
[self.textField1 addTarget:self action:@selector(textField1TextChange:) forControlEvents:UIControlEventEditingChanged];
}
-(void)textField1TextChange:(UITextField *)textField{
NSLog(@"textField1 - 输入框内容改变,当前内容为: %@",textField.text);
}
#pragma mark - NSNotificationCenter 添加监听方法
-(void)addNSNotificationCenter{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textField2TextChange:) name:UITextFieldTextDidChangeNotification object:self.textField2];
}
-(void)textField2TextChange:(NSNotification *)noti{
UITextField *currentTextField = (UITextField *)noti.object;
NSLog(@"textField2 - 输入框内容改变,当前内容为: %@",currentTextField.text);
}
-(void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
-(void)addKVO{
[self.textField3 addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
self.textField3.text = @"123";
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
if ([keyPath isEqualToString:@"text"] && object == self.textField3) {
NSLog(@"textField3 - 输入框内容改变,当前内容为: %@",self.textField3.text);
}else{
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
-(void)dealloc{
[self.textField3 removeObserver:self forKeyPath:@"text" context:nil];
}
#pragma mark - 代理
-(void)addDelegate{
//实现 UITextFieldDelegate 协议
self.textField4.delegate = self;
}
#pragma mark UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
return YES;
}// return NO to disallow editing.
- (void)textFieldDidBeginEditing:(UITextField *)textField{
NSLog(@"textField4 - 开始编辑");
}// became first responder
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
return YES;
}// return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end
- (void)textFieldDidEndEditing:(UITextField *)textField{
NSLog(@"textField4 - 结束编辑");
}// may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called
- (void)textFieldDidEndEditing:(UITextField *)textField reason:(UITextFieldDidEndEditingReason)reason NS_AVAILABLE_IOS(10_0){
}// if implemented, called in place of textFieldDidEndEditing:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
NSLog(@"textField4 - 正在编辑, 当前输入框内容为: %@",textField.text);
return YES;
}// return NO to not change text
作者:Yeso
链接:https://www.jianshu.com/p/2fb5c9b8ea4c
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。