首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UItextField becomeFirstResponder返回否

UItextField becomeFirstResponder返回否
EN

Stack Overflow用户
提问于 2014-01-07 01:48:55
回答 1查看 79关注 0票数 0

我有一个自定义UITableViewCells的UITableView,每个都有两个UITextFields。当我在打开的视图中选择一个UITextField时,也就是没有滚动的情况下,在打开的视图中选择一个UITextField。it becomesFirstResponder和我可以输入文本,当我在打开的视图中点击enter all UITextFields can becomeFirstResponder时,但当我到达打开的视图的UITableViewCells的末尾,并试图加载当前不在视图中的下一个UITableViewCell并将其UITextField设置为becomeFirstResponder时,它不起作用。

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

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

代码语言:javascript
复制
#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;
}
EN

回答 1

Stack Overflow用户

发布于 2014-01-07 01:54:20

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

代码语言:javascript
复制
[tableview setContentOffset:CGPointMake(tableview.contentOffset.x,tableview.contentOffset.y + cellHeight) animated:YES];

你必须根据你的喜好来调整这个滚动,因为我不知道你的应用程序是如何流动的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20956053

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档