【问题标题】:UItextField becomeFirstResponder returning NoUItextField becomeFirstResponder 返回 No
【发布时间】:2014-01-06 17:48:55
【问题描述】:

我有一个自定义 UITableViewCells 的 UITableView,每个都有两个 UITextFields。当我在打开视图中选择 UITextField 时,即不滚动选择打开视图中的 UITextField。它成为FirstResponder,我可以输入文本,因为我在打开视图中输入所有 UITextFields 可以成为FirstResponder 但是当我到达该打开视图的 UITableViewCells 的末尾并尝试加载当前不在视图中的下一个 UITableViewCell 并设置其UITextField to becomeFirstResponder 它不起作用。

我在 textFieldShouldReturn 中设置了一个 if 语句,它确认 textField 正在返回 NO 给 becomeFirstResponder。

我想知道如何解决这个问题;我一直在寻找解决方案,但还没有找到适合我情况的任何东西。下面是我正在使用的 UITextFieldDelegates,因为我认为在这里我应该执行调用或加载下一个 UITableViewCell 之类的操作,但我不知道如何操作。

#pragma mark - Textfield Delegates
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    [textField resignFirstResponder];

    if ([textField isEqual:cell.widthTexField]) {
        nWidth = [[sortedItemsArray objectAtIndex:textField.tag] valueForKey:@"w_MM"];
    } else if ([textField isEqual:cell.heightTextField]) {
        nHeight = [[sortedItemsArray objectAtIndex:textField.tag] valueForKey:@"h_MM"];
    }
    return YES;
}

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

        int height = self.finishingTableView.frame.size.height;
        NSLog(@"%i", height);
         self.finishingTableView.frame= CGRectMake(self.finishingTableView.frame.origin.x, self.finishingTableView.frame.origin.y, self.finishingTableView.frame.size.width, 307);

    // select correct row
    if (textfield.tag > 999999) {
        int adjustTag = textfield.tag-1000000; // remove a million so that you have the text fields correct position in the table. (this is only for height textfields)
        NSIndexPath *indexPath =[NSIndexPath indexPathForRow:adjustTag inSection:0];
        [finishingTableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
        [self tableView:finishingTableView didSelectRowAtIndexPath:indexPath];
        [self.finishingTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionNone animated:YES];
        return YES;
    } else {
        NSIndexPath *indexPath =[NSIndexPath indexPathForRow:textfield.tag inSection:0];
        [finishingTableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
        [self tableView:finishingTableView didSelectRowAtIndexPath:indexPath];
        [self.finishingTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionNone animated:YES];
        return YES;
    }
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSLog(@"%i", textField.tag+1);
    [[self.view viewWithTag:textField.tag+1] becomeFirstResponder];

    BOOL success = [[self.view viewWithTag:textField.tag+1] becomeFirstResponder];
    if (success) {
        NSLog(@"HypnosisView became the first responder");
    } else {
        NSLog(@"Could not become first responder");
    }

    // this means there has been a change in the UItextfield


    NSLog(@"%@", selectedItemDictionary);
    if (textField.tag < 999999) {
        tempFinishingObjectDictionary = [selectedItemDictionary mutableCopy];
        if (![textField.text isEqualToString:[selectedItemDictionary objectForKey:@"w_MM"]]) {
            tempUpdatedRow = @"T";
            // remove kevalues
            [tempFinishingObjectDictionary removeObjectForKey:@"updatedRow"];
            [tempFinishingObjectDictionary removeObjectForKey:@"new_w_MM"];
            // update keyvalues
            [tempFinishingObjectDictionary setValue:tempUpdatedRow forKey:@"updatedRow"];
            [tempFinishingObjectDictionary setValue:textField.text forKey:@"new_w_MM"];
        }
    } else {
        if (![textField.text isEqualToString:[selectedItemDictionary objectForKey:@"h_MM"]]) {
            tempUpdatedRow = @"T";
            // remove kevalues
            [tempFinishingObjectDictionary removeObjectForKey:@"updatedRow"];
            [tempFinishingObjectDictionary removeObjectForKey:@"new_h_MM"];
            // update keyvalues
            [tempFinishingObjectDictionary setValue:tempUpdatedRow forKey:@"updatedRow"];
            [tempFinishingObjectDictionary setValue:textField.text forKey:@"new_h_MM"];
        }
    }
    NSLog(@"%@", tempFinishingObjectDictionary);
    [coreDataController editSelectedFinishing:htmlProjID UpdatedNSD:tempFinishingObjectDictionary SelectedRow:selectedItemIndexPathRow];
    [SVProgressHUD dismiss];


    NSLog(@"%@", selectedItemDictionary);

    return YES;
}

【问题讨论】:

    标签: ios objective-c uitableview uitextfield


    【解决方案1】:

    在您的 textFieldShouldReturn 中,当您转到下一个单元格时,您应该检查是否有两个单元格不在屏幕上(因此不存在)。如果是 iff 屏幕,您应该将表格滚动一个单元格

    [tableview setContentOffset:CGPointMake(tableview.contentOffset.x,tableview.contentOffset.y + cellHeight) animated:YES];
    

    您必须根据自己的喜好调整此滚动,因为我不知道您的应用程序是如何流动的。

    【讨论】:

    • 好的,现在实施会让你知道我的进展。我在 CGPointMake 遇到了一个“问题”,需要在它之前添加一个“[”。
    • 出于某种原因,这对我来说不是滚动的。我已将它放入我的 becomeFirstReasponder if 语句中 textFieldShouldReturn 中,因此如果文本字段为 NULL,那么我打算执行您给我的代码行。但是由于某种原因,它不会为我滚动。我将 cellHeight 设置为 84,所以我会注意到它滚动太多但它不滚动。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多