【发布时间】:2013-06-08 19:29:10
【问题描述】:
我在让文本字段接受 becomeFirstResponder 指令时遇到问题。
我提供了一种自定义机制来在导航栏中创建标题。我有另一个成功使用相同技术的视图控制器。在 viewDidAppear 我开火了:
- (void)addTitleTextField
{
CGRect textFrame = self.parentViewController.navigationController.navigationBar.frame;
textFrame.size.width = 300.0;
textFrame.origin.y = (768.0 - 300.0)/2;
textFrame.size.height = 30.0;
textFrame.origin.x = 7.0;
self.titleTextField = [[UITextField alloc] initWithFrame:textFrame];
self.titleTextField.placeholder = NSLocalizedString(@"New Multiple Choice Quiz", @"New Multiple Choice Quiz");
self.titleTextField.borderStyle = UITextBorderStyleRoundedRect;
self.titleTextField.font = [UIFont boldSystemFontOfSize:20.0];
self.titleTextField.textAlignment = NSTextAlignmentCenter;
self.titleTextField.backgroundColor = [UIColor whiteColor];
self.titleTextField.textColor = [UIColor blackColor];
self.titleTextField.delegate = self;
[self.titleTextField setAutocorrectionType:UITextAutocorrectionTypeNo];
self.titleTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
[self.titleTextField setAutocapitalizationType:UITextAutocapitalizationTypeNone];
self.activeTextField = self.titleTextField;
self.parentViewController.navigationItem.titleView = self.titleTextField;
[self.titleTextField becomeFirstResponder];
}
self.titleTextField 将允许我设置文本值,但如果您使用 canBecomeFirstResponder 检查它会返回 NO。如您所见,我在 parentViewController 上进行了设置。我尝试使用委托来尝试让 parentViewController 设置它。当我执行委托并检查 textField canBecomeFirstResponder 是否返回 YES 时,我仍然无法使其接受 firstResponder 订单。有任何想法吗?文档说“只有当前响应者可以放弃第一响应者状态(canResignFirstResponder)并且新响应者可以成为第一响应者时,响应者对象才会成为第一响应者”。
【问题讨论】:
-
触摸字段时是否弹出键盘?
-
您是否已将
<UITextFieldDelegate>添加到您的类定义中?你设置textfield.delegate = self;了吗? -
您还应该将文本字段添加到您的视图中,使用
addSubView: -
@sangony 是的,键盘弹出。
-
@philosopherdog 如果它解决了您的问题,请接受答案!
标签: ios objective-c cocoa-touch