由于某些原因,我无法将textfield作为第一响应者。UITextView无法正常工作的第一个响应者
我有一个UITableView两行。每一行都有一个标签和一个UITextField。文本域被标记为kLoginRowIndex = 0和kPasswordRowIndex = 1.正如您可能已经猜到的那样,我使用它来设置登录名和密码。
如果用户在编辑登录文本字段时点击返回按钮,我希望密码文本字段获得焦点。不幸的是,密码textfield不接受焦点。这里是我的代码:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
NSLog(@"%s:(textField.tag:%d)", __FUNCTION__, textField.tag);
[textField resignFirstResponder];
if(textField.tag == kLoginRowIndex) {
UITableViewCell *cell = [self tableView:self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:kPasswordRowIndex inSection:0]];
UITextField *nextTextField = (UITextField *)[cell viewWithTag:kPasswordRowIndex];
NSLog(@"(nextTextField.tag:%d)", nextTextField.tag);
NSLog(@"canBecomeFirstResponder returned %d", [nextTextField canBecomeFirstResponder]);
NSLog(@"becomeFirstResponder returned %d", [nextTextField becomeFirstResponder]);
} else {
[self validate:textField];
}
return NO;
}
这是日志输出:
-[SettingsViewController textFieldShouldReturn:]:(textField.tag:0) (nextTextField.tag:1) canBecomeFirstResponder returned 1 becomeFirstResponder returned 0
我试了一下:
- 返回的是不是NO
- 删除调用canBecomeFirstResponder(其仅用于调试目的)
任何提示感谢!
谢谢!虽然不是我正在寻找的东西,但你的回答帮助我指出了正确的方向。 标签可用于识别视图对象。 http://developer.apple.com/iPhone/library/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/doc/uid/TP40006816-CH3-SW25 – 2009-12-13 14:52:35