1

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??

Aberrant
  • 3,423
  • 1
  • 27
  • 38
Andrea Mario Lufino
  • 7,921
  • 12
  • 47
  • 78

6 Answers6

5

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.

jeswang
  • 1,017
  • 14
  • 26
  • I also had issues with the view getting covered up if I called becomeFirstResponder too quickly. I changed the delay to 0.0f and called the line above in viewDidAppear and that sorted things out. Seems like it gets confused if it's called while things are still changing. – Nick Oct 13 '13 at 06:22
1

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.

Eugene
  • 11
  • 1
0

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];

  ....

  }
Gustavo
  • 785
  • 1
  • 12
  • 31
0

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()
    }
Ghulam Rasool
  • 3,996
  • 2
  • 27
  • 40
0

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.

Jonathan Ellis
  • 5,221
  • 2
  • 36
  • 53
  • I thought viewWillAppear is called when the modal view is dismissed. I don't understand what do you mean when you say to pass a reference to MessageComposeViewController. How can i do this? – Andrea Mario Lufino Nov 25 '11 at 13:25
  • Have a look at this question for examples of where `viewWillAppear` might and might not be called: http://stackoverflow.com/questions/131062/iphone-viewwillappear-not-firing If none of those work, then I can provide some code examples but the easiest would be to create an instance variable and property on MessageComposeViewController which after initialisation is given a reference back to the original view controller. – Jonathan Ellis Nov 25 '11 at 13:38
  • MessageViewController is a default controller, how can i create a property on it? – Andrea Mario Lufino Nov 25 '11 at 13:43
-1

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! :)

Andrea Mario Lufino
  • 7,921
  • 12
  • 47
  • 78