1

I am trying to implement "previous," "next," and "done" buttons for a series of UITextFields, each of which is contained in a UITableViewCell in a grouped UITableView. I hold on to the UITextFields in an NSMutableArray, and keep an integer pointing to the UITextField that is currently active. Here are the two selectors that get fired when the Previous and Next buttons are tapped, respectively.

-(IBAction)didSelectPreviousButton:(id)sender
{    
    if ((textFieldIndex - 1) >= 0) {
        UITextField *currentField = [self.testTextFields objectAtIndex:(textFieldIndex)];
        UITextField *nextTextField = [self.testTextFields objectAtIndex:(--textFieldIndex)];
        BOOL result = [nextTextField becomeFirstResponder];
        NSLog([NSString stringWithFormat:@"currentField's window: %@", currentField.window]);
        NSLog([NSString stringWithFormat:@"nextTextField's window: %@", nextTextField.window]);
    } else {
        [self dismissKeyboard:sender];
    }
}

-(IBAction)didSelectNextButton:(id)sender
{
    if ((textFieldIndex + 1) < [self.inspectionItemSpec.numberOfTests intValue]) {
        UITextField *currentField = [self.testTextFields objectAtIndex:(textFieldIndex)];
        UITextField *nextTextField = [self.testTextFields objectAtIndex:(++textFieldIndex)];
        BOOL result = [nextTextField becomeFirstResponder];
        NSLog([NSString stringWithFormat:@"currentField's window: %@", currentField.window]);
        NSLog([NSString stringWithFormat:@"nextTextField's window: %@", nextTextField.window]);
    } else {
        [self dismissKeyboard:sender];
    }
}

As you can see, I am logging the window property of the current & next text field, and in the didSelectNextButton, everything is correct. However, in didSelectPreviousButton, nextTextField.window is always nil. Why would this be happening?

(Note that the previous button is enabled only after the user has tapped the next button once.)

4

2 回答 2

1

这可能是因为每个UITextField都在一段UITableViewCell时间内也被引用self.testTextFields。由于 iOS 中的表格重复使用单元格的方式,您可能(并且可能会)最终遇到数组中的下一个文本字段不是表格中下一个可见行中的文本字段的情况。

如果您发布tableView:cellForRowAtIndexPath:代码,这可能会使问题变得明显。

于 2012-06-06T23:59:53.107 回答
0

窗口是视图层次结构的根。如果 window 属性为 nil,则视图尚未通过类似的方式添加到视图层次结构中

    [someViewInTheViewHierarchy addSubview:yourView].
于 2012-06-06T23:47:26.140 回答