问题
I have a UITableView where I display a list of chat rooms. I'm also using a search bar to find an specific chat room. When a user taps into a cell, it segues to the chat room conversation, displaying the messages and a text box to send messages.
However, if I seach a chat room using the search bar, when I tap in any cell the segue is done but the layout of the chat room is wrong. The conversation view (custom UITableView) fills the whole screen and the text box (located at the bottom, just in top of a TabBar) doesn't appear.
Trying to solve this issue, I've discovered that I was able to segue correctly when searching if I dismissed the keyboard before tapping into any cell, so it seems that this is only happening when the keyboard is present.
I think I can solve this issue programatically dismissing the keyboard before segueing, but I'm also afraid that maybe I'm doing something wrong with the UISearchBaror the UITableView.
My target project is iOS 5.1 and I've reproduced this issue in an iPad 3 running 5.1, iPad 2 running 6.01 and iPhone simulators.
EDIT
This is the code I'm using to segue
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.searchController.searchResultsTableView == tableView) {
self.selectedItem = [self.filteredItems objectAtIndex:[indexPath row]];
} else {
self.selectedItem = [self.items objectAtIndex:[indexPath row]];
}
[self performSegueWithIdentifier:@"segueChatRoom" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
[self.view endEditing:YES]; // This line solves the issue
UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle: @"Custom title" style: UIBarButtonItemStyleBordered target: nil action: nil];
[[self navigationItem] setBackBarButtonItem: newBackButton];
if ([segue.identifier isEqualToString:@"segueChatRoom"]) {
[segue.destinationViewController setItem:self.selectedItem];
}
}
回答1:
As you already pointed out you need to resign the first responder (lower the keyboard). Your [self.view endEditing:YES]; is essentially doing this.
来源:https://stackoverflow.com/questions/13837689/if-keyboard-is-not-dismissed-segueing-when-using-a-search-bar-changes-destinati