gpt4 book ai didi

ios - 无法找到文本字段的下一个响应者

转载 作者:行者123 更新时间:2023-11-28 20:40:24 24 4
gpt4 key购买 nike

我正在尝试循环/导航 UITextField,我将其作为 subview 添加到 UITableViewCell。但是,我无法在 textFieldShouldReturn: 方法中获取我的 nextResponder 值。谁能告诉我我的代码哪里出错了?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString* PlaceholderCellIdentifier = @"PlaceholderCell";

UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:PlaceholderCellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:PlaceholderCellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
}

if (indexPath.row == 0) // first name
{
cell.textLabel.text = @"First Name:";
UITextField *tempFirstNameField = [[UITextField alloc]initWithFrame:CGRectMake(100, (44-18)/2, 320-100, 32)];
self.firstNameField = tempFirstNameField;
self.firstNameField.font = [UIFont systemFontOfSize:14];
self.firstNameField.tag = 1;
self.firstNameField.returnKeyType = UIReturnKeyNext;
self.firstNameField.delegate = self;
[tempFirstNameField release];
[cell.contentView addSubview:self.firstNameField];
}
else if (indexPath.row == 1) //last name
{
cell.textLabel.text = @"Last Name:";
UITextField *tempLastNameField = [[UITextField alloc]initWithFrame:CGRectMake(100, (44-18)/2, 320-100, 32)];
self.lastNameField = tempLastNameField;
self.lastNameField.font = [UIFont systemFontOfSize:14];
self.lastNameField.tag = 2;
self.lastNameField.returnKeyType = UIReturnKeyNext;
self.lastNameField.delegate = self;
[tempLastNameField release];
[cell.contentView addSubview:self.lastNameField];
}

return cell;
}

-(BOOL)textFieldShouldReturn:(UITextField*)textField;
{
NSInteger nextTag = textField.tag + 1;
NSLog(@"next tag %i",nextTag);
// Try to find next responder

UIResponder* nextResponder = [textField.superview.superview viewWithTag:nextTag];
//This always returns me null value
NSLog(@"next responder %@", nextResponder);
if (nextResponder) {
// Found next responder, so set it.
[nextResponder becomeFirstResponder];
} else {
// Not found, so remove keyboard.
[textField resignFirstResponder];
}
return NO; // We do not want UITextField to insert line-breaks.
}

最佳答案

为什么你需要一个 tableView,你的字段似乎是静态的。如果内容比屏幕大,请使用简单的 ScrollView 。要循环您的字段,您可以:

1/为导航循环中所需的所有控件使用容器 View ,并简单地在 subview 中循环 NSArray

2/最佳选择。使用 NSUInteger 标签 字段设置控件获得焦点的顺序。从非零值开始,因为 0 是默认标记值。 10,11,12,13 并在您的容器 View 上使用 viewWithTag: 来检索下一个控件。

关于ios - 无法找到文本字段的下一个响应者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8775544/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com