gpt4 book ai didi

ios - UITextField 不会成为 FirstResponder

转载 作者:可可西里 更新时间:2023-11-01 05:30:17 29 4
gpt4 key购买 nike

我在让文本字段接受 becomeFirstResponder 指令时遇到问题。

我提供了一种自定义机制来在导航栏中创建标题。我有另一个 View Controller 成功地使用了同样的技术。在 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 上进行了设置。我试过使用委托(delegate)来尝试让 parentViewController 对其进行设置。当我执行委托(delegate)并检查 textField canBecomeFirstResponder 是否返回 YES 时,但我仍然无法使其接受 firstResponder 订单。有任何想法吗?文档说“如果当前响应者可以退出第一响应者状态(canResignFirstResponder)并且新响应者可以成为第一响应者,则响应者对象只会成为第一响应者”。

最佳答案

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

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

基本原理:在主线程以外的方法上调用 UIKit 方法(即更新 View )将不起作用。 .这可能正在发生。 (不清楚从哪里调用 addTitleTextField 方法)。

还有其他急救人员需要一些时间辞职吗?

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

基本原理:如果另一个字段卡在 first responder 上(或在辞职过程中),它将通过等待下一个运行循环来给它时间来清理辞职。 . . . .通常下一个运行循环将有足够的时间让前一个响应者清理,或者你可以尝试像 0.05 这样的短暂延迟。

关于ios - UITextField 不会成为 FirstResponder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17003057/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com