在viewDidLoad方法里使用
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
然后调用UITextFieldDelegate代理方法
-(void)keyboardWillShow:(NSNotification*)note
{
CGRect keyBoardRect=[note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
self.tabelView.contentInset=UIEdgeInsetsMake(0,0, keyBoardRect.size.height+100,0);
}
#pragma mark 键盘消失
-(void)keyboardWillHide:(NSNotification*)note{
self.tabelView.contentInset = UIEdgeInsetsZero;
}
如果是cell上的多个textfield的点击事件可以这样写:
//首先在cellForRowAtIndexPath:方法里给textfield绑定tag 添加点击事件
cell.nameTf.tag= indexPath.row;
[cell.nameTf addTarget:self action:@selector(changedTextField:) forControlEvents:UIControlEventEditingChanged];
然后调用点击事件和代理方法
#pragma mark --- target && UITextFieldDelegate
- (void)changedTextField:(UITextField*)textField{
UITextField*tempTextField = (UITextField*)textField;
NSLog(@"tag------%ld;值是---%@",tempTextField.tag,tempTextField.text);
if(tempTextField.tag==0) {
NSString*str = tempTextField.text;
NSLog(@"%@",str);
}elseif(tempTextField.tag==1){
NSString*str = tempTextField.text;
NSLog(@"11===%@",str);
}elseif(tempTextField.tag==2){
NSString*str = tempTextField.text;
NSLog(@"22===%@",str);
}
}
- (BOOL)textFieldShouldReturn:(UITextField*)textField{
//监听键盘return键的点击,隐藏键盘
[textFieldresignFirstResponder];
return YES;
}
网友评论