4

我有一个视图控制器,我有一个表格视图,在表格视图中,当我点击文本字段键盘时,每一行都有文本字段,键盘出现并隐藏表格视图,然后看不到编辑,我该如何解决这个问题问题?我放置了修复视图控制器对齐的观察者,但它在这里不起作用是代码..

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillShow:)
                                             name:UIKeyboardWillShowNotification
                                           object:nil];
 return YES;
}

-(void)keyboardWillShow {
// Animate the current view out of the way
[UIView animateWithDuration:0.3f animations:^ {
    self.viewFrame =  CGRectMake(0, -160, 320, 480);
}];
}
4

2 回答 2

2
you have to write below code, it will hide and show keyboard.   

====>.h file: declare one UITextField as below:

UITextField *actifText;

====>.m file:

-(void)viewDidAppear:(BOOL)animated
{
  // Register notification when the keyboard will be show
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillShow:)
                                             name:UIKeyboardWillShowNotification
                                           object:nil];

// Register notification when the keyboard will be hide
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillHide:)
                                             name:UIKeyboardWillHideNotification
                                           object:nil];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{            
[textField resignFirstResponder];
return YES;
}

-(void)viewDidDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(void) keyboardWillShow:(NSNotification *)note
{
// Get the keyboard size
CGRect keyboardBounds;
[[note.userInfo valueForKey:UIKeyboardFrameBeginUserInfoKey] getValue: &keyboardBounds];

// Detect orientation
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
CGRect frame = self.tblName.frame;

// Start animation
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3f];

// Reduce size of the Table view 
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
    frame.size.height -= keyboardBounds.size.height;
else 
    frame.size.height -= keyboardBounds.size.width;

// Apply new size of table view
self.tblName.frame = frame;

// Scroll the table view to see the TextField just above the keyboard
if (self.actifText)
{
    CGRect textFieldRect = [self.tblName convertRect:self.actifText.bounds fromView:self.actifText];
    [self.tblName scrollRectToVisible:textFieldRect animated:NO];
}

[UIView commitAnimations];
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
self.actifText = nil;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
   self.actifText = textField;
}

-(void) keyboardWillHide:(NSNotification *)note
{
// Get the keyboard size
CGRect keyboardBounds;
[[note.userInfo valueForKey:UIKeyboardFrameBeginUserInfoKey] getValue: &keyboardBounds];

// Detect orientation
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
CGRect frame = self.tblName.frame;

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3f];

// Reduce size of the Table view 
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
    frame.size.height += keyboardBounds.size.height;
else 
    frame.size.height += keyboardBounds.size.width;

// Apply new size of table view
self.tblName.frame = frame;

[UIView commitAnimations];
}
于 2013-08-01T08:35:24.117 回答
0

当您点击单元格内的某个文本字段时,您必须知道当前单元格的索引路径,因此当显示键盘时,您可以对其进行聚焦。

-(BOOL) textFieldShouldBeginEditing:(UITextField *)textField
{
    UITableViewCell *cell = (UITableViewCell *)[textField superview].superview;
    NSIndexPath *idxPath = [table indexPathForCell:cell];
    selectedIndex = idxPath.row;//instance variable

    return YES;
}

-(void)keyboardWillShow {

[table scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:selectedIndex inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];

}

还要添加以下行ViewDidLoad,请记住观察键盘何时出现。如果使用UIKeyboardWillShowNotification, firstkeyboardWillShow将被调用,然后textFieldShouldBeginEditing:是错误的。

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillShow:)
                                             name:UIKeyboardDidShowNotification
                                           object:nil];

并设置

textfield.delegate = self;

cellForRowAtIndexPath创建文本字段时。它将调用所需的方法。

于 2013-08-01T07:41:06.833 回答