Ios 添加[myUITextField becomeFirstResponder];不显示键盘

Ios 添加[myUITextField becomeFirstResponder];不显示键盘,ios,uitextfield,uikeyboard,Ios,Uitextfield,Uikeyboard,我创建了一个屏幕,上面有一个UITextField。当我收到EditingDidBegen事件时,我退出FirstResponder,打开一个带有另一个文本字段的Popover,并为该文本字段调用BecomeFirstResponder 当它运行时,我得到闪烁的插入指针和X清除内容。虽然没有键盘。主UIView设置为UserInteractionEnabled:是 第一个UITextField的目标操作,它在自己的视图中 [textField addTarget:self action:@sel

我创建了一个屏幕,上面有一个UITextField。当我收到EditingDidBegen事件时,我退出FirstResponder,打开一个带有另一个文本字段的Popover,并为该文本字段调用BecomeFirstResponder

当它运行时,我得到闪烁的插入指针和X清除内容。虽然没有键盘。主UIView设置为UserInteractionEnabled:是

第一个UITextField的目标操作,它在自己的视图中

[textField addTarget:self action:@selector(wantsToEditValue:) forControlEvents:UIControlEventEditingDidBegin];
目标操作选择器:

- (IBAction)wantsToEditValue:(id)sender {
// set notification so we can update from popover
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(saWriteValue:)
                                             name:kRefreshFromPopover object:nil];

    //we dont want the TagValue textfield to really be the first responder.
[textField resignFirstResponder];


    [... setup popoverVC, and View. present popover...]
}

下面是创建第二个UITextField的代码。这段代码是在VC中为Popover编写的

- (void)viewDidLoad
{
if (IoUIDebug & IoUIDebugSelectorNames) {
    NSLog(@"%@ - %@", [self description], NSStringFromSelector(_cmd) );
}    [super viewDidLoad];

[self createElementInputControl];
[self createWriteButton];

    //We want the input Focus
     [textFieldInput becomeFirstResponder];


    //Resize our view to handle the new width
CGRect newViewSize = CGRectMake(self.view.frame.origin.x, 
                                self.view.frame.origin.y, 
                                writeButton.frame.origin.x + writeButton.frame.size.width + kWriteElementOffset , 
                                self.view.frame.size.height);

[self.view setFrame:newViewSize];
}
创建输入代码:

-(void) createElementInputControl {


 textFieldInput = [[UITextField alloc] initWithFrame:CGRectMake( kWriteElementOffset   ,
                                                                            kWriteElementHeightOffset, 
                                                                            kTagValueInputInitialWidth,
                                                                            kWriteElementDefaultHeight)];

textFieldInput.borderStyle = UITextBorderStyleRoundedRect;
textFieldInput.clearButtonMode = UITextFieldViewModeWhileEditing;
textFieldInput.textAlignment = UITextAlignmentLeft;
[textFieldInput setDelegate:self];
[textFieldInput setKeyboardType:UIKeyboardTypeDefault];

    // Set the value of the text
[textFieldInput setText:self.myTag.value];

CGSize textFieldInputSize  = [textFieldInput.text sizeWithFont:textFieldInput.font];

    //Set the Button Width
[textFieldInput setFrame:CGRectMake(textFieldInput.frame.origin.x, textFieldInput.frame.origin.y, textFieldInputSize.width + kTagValueInputWidthBuffer, textFieldInput.frame.size.height)];

[self.view addSubview:textFieldInput];
}
当我删除becomeFirstResponder代码时,弹出框显示正常,但没有闪烁的插入指针。我点击字段,得到插入指针,X清除内容按钮,是的,还有一个键盘

我希望显示键盘,而不必在新文本字段中单击


谢谢

要成为第一响应者,视图必须位于视图层次结构中。 您需要将textFieldInput作为子视图添加到某些内容中

根据UIResponder中的苹果文件:

您可以调用此方法使响应者对象(如视图)成为第一响应者。但是,仅当它是视图层次结构的一部分时,才应在该视图上调用它。如果视图的窗口属性包含UIWindow对象,则该对象已安装在视图层次结构中;如果返回nil,则视图将从任何层次分离


当你从didBeginEditing调用becomeFirstResponder时,你将进入一个无限循环。原因是,当你调用becomeFirstResponder时,它调用didBeginEditing。因此,它解释了光标闪烁和您的语句

当我删除becomeFirstResponder代码时,弹出框显示为 正常,但没有闪烁的插入指针。我踏上田野,我得到 插入指针、X清除内容按钮和键盘

为了解决你的问题

在BeginEdit方法中

if(texfield.tag == firstTextFieldTag)
{
//Create second TextField and make it become first responder
}
else
{
// do want you want in the beginEditing of your second textfield.
}

在我的第一个版本的代码中,它不是层次结构的一部分。它现在是视图层次结构的一部分。我在ViewDidLoad中添加了[control becomeFirstResponder]。因此,由于我可以看到控件,我假设它在视图层次结构中。将其添加到viewDidLoad将确保其已在该点添加。通过此更改,它仍然无法正常工作。textFieldInput是如何声明的?我想知道在你的电话成为第一响应者时它是否有一个有效值。另外,您是否尝试过将becomeFirstResponder调用放在CreateElementInputControll的内部?为了更清晰,我确实修改了代码,因此不必包含更多支持代码。其声明如下:
UITextField*textFieldInput=[[UITextField alloc]initWithFrame…
在将textFieldInput添加到视图后,我释放了textFieldInput,然后传回指向它的指针,以便以后可以访问它。becomeFirstResponder最初位于createElementInputControl中。我认为这不适用于我的代码。我编辑了OP以包含更多支持代码。在DidBeginEdit中,我只是辞职FirstRes思考,然后调用管理第二个UITextField的popover。