質問

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.

役に立ちましたか?

解決

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top