Adding [myUITextField becomeFirstResponder]; does not bring up keyboard

女生的网名这么多〃 提交于 2019-12-11 04:30:47

问题


I've created a screen that has a UITextField on it. When I get a EditingDidBegin event, I resignFirstResponder, Bring up a Popover with another textField in it and for that TextField call the BecomeFirstResponder on it.

When it runs, I get Blinking insertion pointer and the X clear contents. Though no Keyboard. The Master UIView is set to UserInteractionEnabled:YES.

target action for First UITextField, its on its own view.

[textField addTarget:self action:@selector(wantsToEditValue:) forControlEvents:UIControlEventEditingDidBegin];

Target Action selector:

- (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...]

}

Here is the code to create the 2nd UITextField. This code is in the VC for the 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];
}

Create Input Code:

-(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];
}

When I remove the becomeFirstResponder code, the Popover come up as normal, though no blinking insertion pointer. I tap the field, I get Insertion Pointer, X clear content button, and yes a Keyboard.

I want the keyboard to show without having to click in the new Text Field.

Thanks!


回答1:


In order to become a first responder, the view must be in the view hierarchy. You need to add your textFieldInput as a subview to something.

As per Apple's doc in 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.




回答2:


When you call the become first responder from the didBeginEditing you will run into an infinite loop.Reason being, when you call becomeFirstResponder it calls didBeginEditing. So It Explains the cursor blink and your statement

When I remove the becomeFirstResponder code, the Popover come up as normal, though no blinking insertion pointer. I tap the field, I get Insertion Pointer, X clear content button, and yes a Keyboard.

To solve your problem,

In the beginEditingMethod ,

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


来源:https://stackoverflow.com/questions/9221601/adding-myuitextfield-becomefirstresponder-does-not-bring-up-keyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!