0

我有一个故事板,我在其中插入了一个滚动条,因为要显示的内容太多,无法一次全部出现。滚动条包含:

  • 图像视图
  • 一个标签
  • 分组表视图

表格视图的行有两种。第一种(由控制器 regularRegistrationCellVC 定义)包含一个文本字段和一个标签。当我单击任何生成的单元格中的任何文本字段时,键盘会出现并且文本字段会被隐藏。此外,无法向上滚动视图的内容以查看键盘下方的内容。因此,按照苹果的指令,我将此代码添加到包含滚动条的控制器中:

注册键盘通知

- (void)registerForKeyboardNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillBeHidden:)
                                                 name:UIKeyboardWillHideNotification object:nil];
}

在发送 UIKeyboardDidShowNotification 时调用

- (void)keyboardWasShown:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
    scrollPanel.contentInset = contentInsets;
    scrollPanel.scrollIndicatorInsets = contentInsets;

    // If active text field is hidden by keyboard, scroll it so it's visible
    // Your application might not need or want this behavior.
    CGRect aRect = self.view.frame;
    aRect.size.height -= kbSize.height;
    if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
        CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y-kbSize.height);
        [scrollPanel setContentOffset:scrollPoint animated:YES];
    }
}

键盘消失时调用

- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    scrollPanel.contentInset = contentInsets;
    scrollPanel.scrollIndicatorInsets = contentInsets;
}

管理编辑开始的事件

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    activeField = textField;
    NSLog(@"inizio digitazione\n");
}

结束编辑事件的管理

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    activeField = nil;
}

其中activeField是我的控制器的全局变量。

然后,我修改了创建第一种单元格的代码部分,这样每个新的第一种单元格都有我的视图控制器作为委托:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section == 0) {
        static NSString *CellIdentifier = @"regularRegistrationCell";

        regularRegistrationCellVC *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[regularRegistrationCellVC alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }

        cell.regularFieldName.text = [self.orientationItems objectAtIndex:indexPath.row];
        cell.regularTextField.delegate = self;
        return cell;

    }

    else{
        static NSString *CellIdentifier = @"orientationRegistrationCell";

        orientationRegistrationCellVC *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[orientationRegistrationCellVC alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }

        cell.fieldLabel.text = [self.orientationItems objectAtIndex:[orientationItems count]-1];

        NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
        NSString *val = nil;

        if (standardUserDefaults)
            val = [standardUserDefaults objectForKey:@"orientation"];

        if (val == nil) 
            cell.orientationLabel.text = @"Eterosessuale";

        else
            cell.orientationLabel.text = val;

        return cell;

    }
}

最后,我声明我的视图控制器(包括滚动条及其内容)实现了 UITextFieldDelegate。使用 NSLog 打印,我发现每次单击文本字段时,肯定会执行管理事件的功能。

你能告诉我哪里错了吗?

感谢帮助

4

1 回答 1

0

您是否尝试过TPKeyboardAvoiding

只是为了在这个答案中修改几行。继续阅读

于 2014-02-12T21:13:49.827 回答