gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:53:32 25 4
gpt4 key购买 nike

我创建了一个屏幕,上面有一个 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/

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