2

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!

scooter133
  • 1,297
  • 1
  • 15
  • 29

2 Answers2

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.

Rayfleck
  • 12,116
  • 8
  • 48
  • 74
  • It was not part of the hierarchy on my first version of the code. Its part of the view hierarchy now. I added the [control becomeFirstResponder] in the ViewDidLoad. So since I can see the Control, I would assume its in the view hierarchy. Adding it to the viewDidLoad would make sure its already added at that point. With this change, it is still not working correctly. – scooter133 Feb 10 '12 at 15:45
  • How is textFieldInput declared? I'm wondering if it's got a valid value at the point your call becomeFirstResponder. Also, have you tried putting the becomeFirstResponder call inside of createElementInputControl – Rayfleck Feb 10 '12 at 16:05
  • I did alter the code for some clarity and so I didn't have to include more supporting code. its declared like: `UITextField *textFieldInput = [[UITextField alloc] initWithFrame...` and I release textFieldInput after adding it to the view, then pass back a pointer to it so I can access it later. the becomeFirstResponder was originally in the createElementInputControl. – scooter133 Feb 10 '12 at 17:00
0

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.
}
Vignesh
  • 10,205
  • 2
  • 35
  • 73
  • I don't think this applies to my code. I edited the OP to include more supporting code. In the didBeginEditing, i just resignFirstResponder, then call a popover that manages the 2nd UITextField. – scooter133 Feb 10 '12 at 15:47