Question

I have a ConfigureViewController that contains some UIButtons and a UITextField * MyTextField. Each of the buttons, when pressed, brings up a dialog-style viewcontroller using presentViewController:animated:completion:. However, if I tap one of those buttons while editing the text field, when i close the dialog and return to the original screen, i am unable to return focus to or type in the text field.

This is the method that is invoked when the button is tapped.

-(void)AdvancedInfoButtonPressed :(id)sender
{
    AdvancedInfoPopViewController *myAdvancedInfoViewController = [[AdvancedInfoPopViewController alloc] init];
    [myAdvancedInfoViewController setDelegateAndDevice :self :Current_Device];
    myAdvancedInfoViewController.modalPresentationStyle = UIModalPresentationFormSheet;
    [self presentViewController :myAdvancedInfoViewController animated :NO completion :nil];
}

Without explicitly removing focus from MyTextField, presenting the AdvancedInfoViewController does dismiss the keyboard automatically.

I suspect the problem is that MyTextField still thinks it has focus (even though the keyboard and blinking cursor have disappeared) and so does not allow itself to become the first responder again. Along these lines, I have found that if i add [MyTextField resignFirstResponder] before presenting the dialog viewcontroller, the problem goes away.

However, this does not seem like a very good solution because it means having to remember to resign this textfield (or any other text fields) as the first responder in several places (leading to code that is difficult to maintain). My question is: are there any events i can hook into either when ConfigureViewController is about to be partially obscured by AdvancedInfoViewController (or when AdvancedInfoViewController is dismissed and focus is returned to the ConfigureViewController) in which i can add some logic to clean up MyTextField's firstResponder status?

I've tried viewWillDisappear and viewWillAppear but they are never called on the ConfigureViewController.

I've also tried adding textFieldDidEndEnding to the text field's delegate but, despite it being called, it did not fix the problem.

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    [MyTextField resignFirstResponder];
}
Was it helpful?

Solution

You can use [self.view endEditing:YES] to resign all first responders in the case you are presenting myAdvancedInfoViewController.

To return focus to the textField after this event occurs you will need to keep track of which textField was active at the time myAdvancedInfoViewController was presented. When the myAdvancedInfoViewController is dismissed call UITextField's becomeFirstResponder method for the appropriate text field.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top