Question

This is the problem : when MessageComposeViewController has been dismissed, my textView doesn't become the first responder and the keyboard doesn't appear. Why? I put the [textView becomeFirstResponder] code in viewWillAppear. How can i do??

Was it helpful?

Solution 6

I found the solution!!!!!!!! So, in the messageComposeViewController:didFinishWithResult delegate method we call [self dismissModalViewControllerAnimated:YES], but it's wrong! This method should receive NO, not YES! And then recall the [textView becomeFirstResponder] method! I don't know why, but in this way the app works perfectly! Thanks all however! :)

OTHER TIPS

I called becomeFirstResponder later by this line:

[self.searchBar performSelector:@selector(becomeFirstResponder) withObject:nil afterDelay:0.1f];

It worked for me. Hope this help.

I guess it's the animation's reason.

According to the Note from the official Apple documentation :

Make sure that your app has established its object graph before assigning an object to be the first responder. For example, you typically call the becomeFirstResponder method in an override of the viewDidAppear: method. If you try to assign the first responder in viewWillAppear:, your object graph is not yet established, so the becomeFirstResponder method returns NO.

you should call [textView becomeFirstResponder] in the viewDidAppear: instead of viewWillAppear:.

Seems the solution you have found with [self dismissModalViewControllerAnimated:NO] is utilizing the same rule from the note.

Your question isn't particularly clear, but you have assumed that viewWillAppear is called when a modal view controller (what I assume you have called MessageComposeViewController) is dismissed. This may not be the case under all cirumstances (e.g. if your view is inside a UINavigationController)

There are a number of ways you could handle this, but perhaps the simplest would be to pass your MessageComposeViewController a reference to your original view controller and call a method to make your UITextField the first responder.

I have more or less the same problem, it was related with animation at the segue, it looks like doing a segue o dismiss a view without using the resignFirstResponder break the relation with the view, I just modify the segue to doing it programatically like this:

  - (IBAction)back:(id)sender {

     [textView resignFirstResponder];
     [self performSegueWithIdentifier:@"returnScreen" sender:self];
  }

An then in the viewWillAppear I did the next:

 -(void)viewWillAppear:(BOOL)animated{
     [super viewWillAppear:animated];
     [textView becomeFirstResponder];

  ....

  }

In swift

let when = dispatch_time(DISPATCH_TIME_NOW, Int64(0.1 * Double(NSEC_PER_SEC)))
    dispatch_after(when, dispatch_get_main_queue()) {
        self.searchBar.becomeFirstResponder()
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top