[英]Why is UITextField's window nil?
I am trying to implement "previous," "next," and "done" buttons for a series of UITextField
s, each of which is contained in a UITableViewCell
in a grouped UITableView
. 我正在尝试为一系列
UITextField
实现“上一个”,“下一个”和“完成”按钮,每个按钮都包含在分组的UITableViewCell
中的UITableView
。 I hold on to the UITextFields
in an NSMutableArray
, and keep an integer pointing to the UITextField
that is currently active. 我坚持使用
NSMutableArray
的UITextFields
,并保留一个指向当前处于活动状态的UITextField
的整数。 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. 如您所见,我正在记录当前和下一个文本字段的window属性,并且在
didSelectNextButton
,一切正确。 However, in didSelectPreviousButton
, nextTextField.window
is always nil
. 但是,在
didSelectPreviousButton
, nextTextField.window
始终为nil
。 Why would this be happening? 为什么会这样呢?
(Note that the previous button is enabled only after the user has tapped the next button once.) (请注意,只有在用户轻按了下一个按钮一次之后,才能启用上一个按钮。)
This may be because each UITextField
is in a UITableViewCell
while also being referenced in self.testTextFields
. 这可能是因为每个
UITextField
都位于UITableViewCell
同时也在self.testTextFields
进行了引用。 Because of the way cells are re-used by tables in iOS, you could (and probably will) end up in a situation where the next text field in your array is not the text field in the next visible row in the table. 由于iOS中的表格可以重复使用单元格的方式,因此您可能(很可能会)遇到以下情况:数组中的下一个文本字段不是表格中下一个可见行的文本字段。
If you post your tableView:cellForRowAtIndexPath:
code, that may make the problem apparent. 如果您发布
tableView:cellForRowAtIndexPath:
代码,则可能使问题显而易见。
The window is the root of the view hierarchy. 窗口是视图层次结构的根。 If the window property is nil, the view hasn't been added to the view hierarchy via something like
如果window属性为nil,则尚未通过以下方式将视图添加到视图层次结构中
[someViewInTheViewHierarchy addSubview:yourView].
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.