2012-01-08 34 views
0

我試圖週期/通過UITextField S的我添加爲子視圖到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. 
} 

回答

2

爲什麼你需要一個的tableView,你的領域似乎是靜態的。如果內容大於屏幕,請使用簡單的滾動視圖。 要循環領域,您可以:

1 /使用您在導航循環希望所有控件的容器視圖,只是循環在你的子視圖NSArray

2 /最佳選擇。使用字段可設置控件應獲得焦點的順序。從非零值開始,因爲0是默認標記值。 10,11,12,13和使用viewWithTag:你的容器視圖來檢索下一個控件。

1

UITableView不是一個數組 - 它可能會重新加載甚至釋放任何單元格,當它不可見時。

如果你想操作生成的單元格 - 這是更好地創建所有這些,放在一個陣列,然後從陣列顯示它們。即在表開始加載之前創建它們,但不在cellForRowAtIndexPath方法中。例如,它可以在ViewWillAppear中完成。

在這種情況下你所有的物體都會被陣列保留,並沒有公佈,直到你想。

相關問題