iosuiviewcontrollerios7uitextfieldkeyboard-navigation

Keybord will not close after back from viewController in iOS 7.0


I have one ViewController with custom view class. I have handled keyboard notification by adding into viewWillAppear method and remove notification in viewDidDisappear.

Notification adding into viewWillAppear:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector (keyboardWillShow:)
                                             name: UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector (keyboardWillHide)
                                             name: UIKeyboardWillHideNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector (keyboardDidHide)
                                             name: UIKeyboardDidHideNotification object:nil];

Remove Notification into viewDidDisappear:

[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object: nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object: nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidHideNotification object: nil];

and call view's method from viewController's notification method.

Issue:

If I remove notification code then worked perfectly.

Before iOS 7.0, its works perfectly with notification code. But in iOS 7.0, its not work.

I have print NSLog in each notification method, in IOS 7.0 keyboardWillShow method executes at last and again keyboard appear. But in iOS 6.0, keyboardWillShow not execute at last.

I have also implements UITextFieldDelegate methods.

Thanks in advance


Solution

  • Below you can see difference in keyboard life cycle for retained (will not dealloc after pop) pushed viewController with textField/textView which becomes the firstResponder.

    Let we add keyboard observers as follows

    - (void) viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
    
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
    }
    
    - (void) viewDidDisappear:(BOOL)animated
    {
        [super viewDidDisappear:animated];
    
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidHideNotification object:nil];
    }    
    

    iOS6 keyboard life cycle for iPhone 5

    iOS7 keyboard life cycle for iPhone 5

    The difference is that iOS7 formally tries to hide keyboard pop but that shows keyboard again.

    Thus if you want to hide keyboard in both cases you need to resign textField explicitly in viewWillDisappear. Note that textField.isFirstResponder equals NO in viewWillDisappear but

    [textField resignFirstResponder]; 
    

    solves the problem.