5

我在让文本字段接受 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)并且新响应者可以成为第一响应者时,响应者对象才会成为第一响应者”。

4

3 回答 3

6

你是在告诉 UITextField 成为后台线程上的选择器吗?

[textField performSelectorOnMainThread:@selector(becomeFirstResponder) withObject:nil 
  waitUntilDone:YES];

Rationale: Calling UIKit methods (ie updating the view) on a method other than the main thread won't work. . this could be happening. (It is not clear where the addTitleTextField method is being called from).

Is there another first responder that needs some time to resign?

[textField performSelectorOnMainThread:@selector(becomeFirstResponder) withObject:nil afterDelay:0.0];

Rationale: If another field is hanging onto first responder (or in the process of resigning it), it will give it time to clean up an resign, by waiting until the next run-loop. . . . . usually the next run-loop will be enough time for the previous responder to clean up, or you could try a short delay like 0.05.

于 2013-06-09T00:55:27.217 回答
0

You might need to add textField to view hierarchy:

v.addSubview(textField)
于 2014-08-18T09:10:16.687 回答
0

Another thing to do is to make sure the textField is enabled (check Interface Builder, then any disabling you might do through code).

I spent almost an hour trying to figure out what the problem was: I got a workaround by subclassing UITextField and overriding canBecomeFirstResponder to always return true. This forced it to finally show keyboard and edit the textField, but I noticed I couldn't tap/select/copy/paste the textField. User Interaction was enabled, and then I finally saw the Enabled not being checked...I blame Apple for not displaying any visual cue when a piece of UI is disabled (like the do with Hidden) lol

于 2018-07-21T19:27:39.870 回答