fabian789
fabian789

Reputation: 8412

Keyboard not showing after MFMessageComposeViewController

In an iPhone app I have a UITextView and a button, which lets the user send the content of the UITextView as a text message. The code looks like this:

MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
picker.messageComposeDelegate = self;

picker.body = textView.text;

[self presentModalViewController:picker animated:YES];

Everything works fine, except for when the message is either sent or Cancel is tapped in the MFMessageComposer: The keyboard for the UITextView is not shown anymore, even though the cursor blinks.

I tried a few things, including a [textView resignFirstRepsonder] in both the button code and -viewDidDisappear. [textView becomeFirstResponder] in the MFMessageComposeViewControllerDelegate method or the -viewDidAppear didn't change anything either...

Any ideas?

Upvotes: 3

Views: 3025

Answers (7)

Nikolay Mikhaylov
Nikolay Mikhaylov

Reputation: 68

This behaviour will not appear if viewController which is shown before modal VC is a child of navigation controller. So solution is to make fake UINavigationController and add your VC controller to nav controller.

Upvotes: 0

mightylost
mightylost

Reputation: 86

As of iOS 5, here is one workaround. Before you present the MFMessageComposeViewController instance, resign first responder on your UITextView:

[self presentViewController:messageComposer animated:YES completion:NULL];
[textView resignFirstResponder];

Then in the delegate method messageComposeViewController:didFinishWithResult: do this:

[controller dismissViewControllerAnimated:YES completion:^{
  [textView performSelector:@selector(becomeFirstResponder) withObject:nil afterDelay:0];
}];

This fixed the disappearing keyboard problem for me. Without having to permanently dismiss the keyboard.

Upvotes: 0

user1004485
user1004485

Reputation: 11

The delay trick also solves the problem of missing text cursor after showing an UIAlert right after MFMessageComposeViewController finishes, however the delay needs to be much longer (0.5 sec in my case)

Upvotes: 1

jasoncrawford
jasoncrawford

Reputation: 2763

I had a similar problem and was able to fix it by calling becomeFirstResponder after a slight delay:

[textField performSelector:@selector(becomeFirstResponder) withObject:nil afterDelay:0.01];

Upvotes: 1

chanderson0
chanderson0

Reputation: 101

I had the same issue, and was resigned to accepting fabian's solution, but found that by calling [self dismissModalViewControllerAnimated:NO] and then calling [textView becomeFirstResponder], I was able to make the keyboard reappear. Something about the animation was screwing up the keyboard; looks like a bug in iOS 4.2.

Upvotes: 6

fabian789
fabian789

Reputation: 8412

I was not able to find a better solution, so here is my fix:

In

- (void) actionSheet:(UIActionSheet *)actionSheet 
willDismissWithButtonIndex:(NSInteger)buttonIndex

I dismiss the Keyboard and in

- (void) actionSheet:(UIActionSheet *)actionSheet 
didDismissWithButtonIndex:(NSInteger)buttonIndex`

I present the MFMessageComposeViewController.

In

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller 
             didFinishWithResult:(MessageComposeResult)result

I don't do [textView becomeFirstResponder] as it doesn't work. Neither does it work in viewDidAppear:. The user has to tap the UITextField again.

Not a very nice solution but the only one I found...

Upvotes: 0

Aurum Aquila
Aurum Aquila

Reputation: 9126

After the view has disappeared, you need to make your view first responder. Add the MFMessageComposeViewControllerDelegate protocol to your header, then use the following:

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
    [self dismissModalViewControllerAnimated:YES];
    [self becomeFirstResponder];
}

Happy coding,

Zane

Upvotes: 1

Related Questions