Adding [myUITextField to become FirstResponder]; does not raise the keyboard

I created a screen that has a UITextField. When I get the EditingDidBegin event, I resignFirstResponder, create a Popover with another text field in it, and for that, TextField calls BecomeFirstResponder.

When it starts, I get a Flashing insert pointer and clear X content. Although there is no keyboard. The UIView wizard is set to UserInteractionEnabled: YES.

target action for the first UITextField, 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 for creating the second UITextField. This code is in VC for 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 delete the startFirstResponder code, the Popover appears as usual, although the insert pointer does not blink. I touch the field, I get an input pointer, an X-clear button and yes a keyboard.

I want the keyboard to appear without having to click in a new text box.

Thanks!

+2
source share
2 answers

To become the first responder, the view must be in the hierarchy of views. You need to add your textFieldInput as a subobject.

According to Apple doc in UIResponder:

You can call this method to create a responder object, such as the first responder view. However, you should only call it on this view if it is part of the view hierarchy. If the views window property contains a UIWindow object, it was set in the view hierarchy; if it returns nil, the view is separated from any hierarchy.

+1
source

When you call the first responder from didBeginEditing, you start an infinite loop. In doing so, when you call startFirstResponder, it calls didBeginEditing. So he explains the blinking cursor and your statement

When I delete the startFirstResponder code, the Popover appears as a normal, albeit not blinking, insertion pointer. I use the field, I get the Insert pointer, the clear X content button, and yes the keyboard.

To solve your problem,

In 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. } 
0
source

Source: https://habr.com/ru/post/927133/


All Articles