0
votes

I had issue that every time when I enter a view controller - which is the "MessageListViewController" in the following screen-shot, the keyboard will be automatically popped out.

The thing is I used a UITextView in this MessageListViewController for text input, and when I pop this view controller from navigationController stack, if I leave the keyboard open, then the next time when it was pushed again, this keyboard will be popped out automatically.

If I make the text view resignFirstResponder before I popped out this view controller, then the next time enter it will be fine. However resignFirstResponder manually will make the keyboard dismissing with an ugly animation (manually dismiss keyboard it will make the keyboard always go downing the screen vertically, however the view controller popping transaction is horizontal, so it will look very wired)

I tried to build a simple sample to test the keyboard dismiss behavior - there are two view controllers, one is the rootViewController of the navigationController, and it will push another into the viewController stack, so the pushed view controller contains a UITextView, if I highlight the text view of the pushed one, then go back to the root view controller, the animation works just fine, also the keyboard won't be automatically popped out. So I just don't know why in my project, this keyboard always shows up unexpectedly if I just left the page without manually dismiss the keyboard.

BTW, the following call stack happened between viewWillAppear and viewDidAppear. It looks like triggered by iOS to restore the last state of the UITextView.

 - (UIView*)findFirstResponder {
  if (self.isFirstResponder) {
    return self;
  }

  for (UIView* subview in self.subviews) {
    UIView* ret = [subview findFirstResponder];
    if (ret) {
      return ret;
    }
  }

  return nil;
}

NSLog(@"%@", [[[UIApplication sharedApplication] keyWindow] findFirstResponder]);

I also tried to track with the key window's first responder when this call stack triggered by keyboard will show notification, and the out put is just "nil".

Call stack of the keyboard will show notification catched

2
use [self.view endEditing:YES] in viewWillAppear method and see whether it works or not.Puneet Sharma
Try to resign your keyboard while you are coming out from the view. -(void)ViewwillDisappear;Romance
Thanks Puneet, I tried to put this in many place, but it still doesn't work.Pei

2 Answers

1
votes

Thanks guys for helping, I just realized where the problem is, actually I did not declare that the messagelistViewController which I used here is actually a singleton, so it never release even I pop the this out of the viewController stack.
Under that scenario, when the next time this view controller was appear again, the UIKit will be smart enough to restore the previous view state if it's not dealloced.
And in my test sample, I did not keep the test viewcontroller when it was popped out of stack. so that's why this works fine for my test sample.
Then my final solution is kinda simple, just remove the textview from it's superview when the view did disappear, and then re-add this back when the view will appear.
It turns out my question is a little bit stupid, however it do let me know two things:

1) The view controller will restore to it's previous state every time when it appears if you not delete it

2) The keyboard dismiss animation will always be going down if manually resign the input from the first responder, if we don't want this effect, we shall never dismiss it manually.

1
votes

I created a demo in iOS 7 as per you said in your question, it was working fine for me even I didn't have to call resignFirstResponder method for resigning keyboard. So you could look in your code as you might have been calling becomeFirstResponder somewhere making the keyboard to be active again or simply post your code here so I could have a look at it.