ios - 添加 [myUITextField becomeFirstResponder];不调出键盘

标签 ios uitextfield uikeyboard

我创建了一个屏幕,上面有一个 UITextField。当我收到 EditingDidBegin 事件时,我 resignFirstResponder,调出一个带有另一个 textField 的 Popover,并为该 TextField 调用 BecomeFirstResponder。

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

第一个 UITextField 的目标操作,它在自己的 View 中。

[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 的代码。此代码在 Popover 的 VC 中..

- (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 代码时,Popover 正常出现,但没有闪烁的插入指针。我点击该字段,我得到插入指针、X 清除内容按钮,是的,还有一个键盘。

我希望无需单击新文本字段即可显示键盘。

谢谢!

最佳答案

为了成为第一响应者, View 必须在 View 层次结构中。 您需要将 textFieldInput 作为 subview 添加到某些内容。

根据 Apple 在 UIResponder 中的文档:

You may call this method to make a responder object such as a view the first responder. However, you should only call it on that view if it is part of a view hierarchy. If the view’s window property holds a UIWindow object, it has been installed in a view hierarchy; if it returns nil, the view is detached from any hierarchy.

关于ios - 添加 [myUITextField becomeFirstResponder];不调出键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9221601/

相关文章:

ios - 选择表情符号时键盘顶部出现随机蓝色

ios - UiTableView cellForRowAt 没有返回好的值并且无法滚动

iphone - 如何更改 UITextField 的占位符颜色?

ios - 从某个 View Controller 导航时如何记住键盘状态?

ios - 自动建议在UITextfield中使用逗号分隔

ios - 如何将标签添加到 textField 类或动画占位符

uikit - UIAlertView 崩溃 iOS7 - 断言失败

ios - Swift NSURLSession 真的真的真的很慢

iphone - Xcode项目管理: adding a iPad (Storyboards, iOS 5)到兼容iOS 4的iPhone项目

ios - 转换层上的 UIGestureRecognizer