-1

I am working on custom table cell, which have some textfields. on some button press method i dynamically add/remove row. But when Keyboard is shown on the screen and button is pressed then application crashes.

Amir iDev
  • 1,257
  • 2
  • 15
  • 29

1 Answers1

3

I use the very simple two lines method to solve the issue

First write a Bool isKeyBoardHide.

Then in ViewDidLoad Write this code

// Listen for keyboard appearances and disappearances
[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(keyboardDidShow:)
                                             name:UIKeyboardDidShowNotification
                                           object:nil];

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

Write these two methods to update bool about current status

- (void)keyboardDidShow: (NSNotification *) notif{
    isKeyBoardHide = NO;
}

- (void)keyboardDidHide: (NSNotification *) notif{
    isKeyBoardHide = YES;
}

When you want to check just implement that code

if(!isKeyBoardHide) {
// Dismiss Keyboard
[self.view endEditing:YES] 
} else {

//keyboard is already hidden
}

Very simple and easy way to solve this crash.

Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
Amir iDev
  • 1,257
  • 2
  • 15
  • 29
  • 1
    What is new then http://stackoverflow.com/questions/1490573/how-to-programatically-check-whether-a-keyboard-is-present-in-iphone-app ? – iPatel Sep 06 '13 at 13:12