Question

I have a UIViewController then with this code I present a modal view controller for entering the password.

[self performSegueWithIdentifier:@"SeguePassword" sender:self];

in Storyboard:

Storyboard Segue Identifier = SeguePassword Style = Modal Transition = default Animates = not checked

When I click "Cancel" on the modal which has the following code:

[self dismissViewControllerAnimated:NO completion:^{}];

Now when I get back the keyboard is hidden. I have code to show a toolbar with a button called hide. And I see that, but not the keyboard.

Does anyone have any ideas or directives they have taken to fix an issue like this? It seems it recently started after converting changes for iOS 7.

My solution:

I found this because after I clicked the but on an alert box a few times out of frustration I saw the keyboard in the wrong orientation. I.e.., a landscape keyboard when the app is portrait only on iPhone.

Before:

- (NSUInteger) supportedInterfaceOrientations {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    return UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight;
} else {
    **return (UIInterfaceOrientationPortrait);**
}

}

After:

- (NSUInteger) supportedInterfaceOrientations {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    return UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight;
} else {
    **return (UIInterfaceOrientationMaskPortrait);**
}

}

I changed the Portrait to use the Mask integer and everything started working again.

Was it helpful?

Solution

The keyboard is only visible if some object is the firstResponder. Look at the documentation for the UIResponder class.

If your modal view controller uses the keyboard, which it sounds like it does, it takes first responder status and whatever had it on your original screen loses it. If you want to return to the same state from the modal view controller, with some object having input focus -- i.e. being the firstResponder -- with the keyboard visible you will have to have write code to make that happen. In particular, your original view controller has to know when the modal view controller terminates so that it can make your input object, textField or textView or whatever, be the first responder with the statement:

   [objectIWantToBeFirstResponder becomeFirstResponder];

Now how do you know when the modal view controller is done? There are a couple obvious techniques.

If you want to use a segue then the modal view controller will have to explicitly let the presenting view controller know that it is terminating. You will probably want to define a protocol in the modal view controller and have the presenting view controller adopt it. When the modal view controller wants to exit it first tells the presenting view controller.

Since you are manually triggering the segue perhaps you're willing to present the modal view controller in code. If so then you can use a completion block:

   ModalViewControllerClass *vc = [[ModalViewControllerClass alloc] init];
   // whatever else you need to do to the vc
    [self presentViewController:vc animated:YESorNO completion:^{
        // This code gets executed when the presented view controller exits
        [objectIWantToBeFirstResponder becomeFirstResponder];
    }];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top