UITextField不会成为FirstResponder [英] UITextField won't becomeFirstResponder

查看:99
本文介绍了UITextField不会成为FirstResponder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在获取文本字段来接受beginFirstResponder指令时遇到问题。

I'm having a problem getting a textfield to accept the becomeFirstResponder directive.

我提供了一种自定义机制,可以在导航栏中创建标题。我有另一个成功使用此相同技术的viewcontroller。在viewDidAppear上,我启动:

I'm providing a custom mechanism to create a title in the navigation bar. I have another viewcontroller that is successfully using this same technique. On viewDidAppear I fire off:

- (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顺序。有任何想法吗? Docs说:如果当前响应者可以辞退第一响应者状态(canResignFirstResponder),而新的响应者可以成为第一响应者,那么响应者对象只会成为第一响应者。

self.titleTextField will allow me to set the text value, but if you check using canBecomeFirstResponder it returns NO. As you can see I am setting this on the parentViewController. I've tried using a delegate to attempt to get the parentViewController to set it. When I do the delegate and check whether the textField canBecomeFirstResponder it returns YES, but I'm still unable to make it accept the firstResponder order. Any ideas? The Docs say "A responder object only becomes the first responder if the current responder can resign first-responder status (canResignFirstResponder) and the new responder can become first responder".

推荐答案

您是否要让UITextField成为后台线程的选择器?

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

比率:在方法上调用UIKit方法(即更新视图)主线程以外的其他线程将无法正常工作。 。这可能正在发生。 (尚不清楚从何处调用addTitleTextField方法)。

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).

是否有另一个急救人员需要一些时间辞职?

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

比率:如果另一个字段挂在第一响应者上(或在辞职的过程),它会等到下一个运行循环,才有时间清理辞职。 。 。 。 。通常,下一个运行循环将有足够的时间供前一个响应者清理,或者您可以尝试使用诸如0.05之类的短暂延迟。

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.

这篇关于UITextField不会成为FirstResponder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆