单击导航栏按钮时,我以模态方式呈现视图。此视图具有tableview视图。单击tableview行时,视图将被关闭。当我添加UISearchController时遇到问题。当我从搜索栏的结果中单击某个项目时,它会以模态方式消失但在其后面是相同的视图,但是tableview正在显示完全填充的视图。一秒后,视图以模态消失,我回到导航视图。我已经包含了一个gif来帮助直观地解释我的问题。这是代码片段。
在具有UISearchBar的视图控制器中:
在方法cellforRowAtIndexPath
中 //Check to see if results for search bar
if ([self.searchController isActive] && ![self.searchController.searchBar.text isEqualToString:@""]) {
restaurantCellData = self.searchResults[indexPath.row];
} else {
restaurantCellData = self.restaurantLocations[indexPath.row];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
RestaurantLocationData *restaurantCellData = (RestaurantLocationData *)[self.restaurantLocations objectAtIndex:indexPath.row];
if (self.restaurantSelectedCallBack) {
self.restaurantSelectedCallBack(restaurantCellData);
}
[self.nearByTableView deselectRowAtIndexPath:indexPath animated:YES];
}
在呈现视图控制器中,我有回调块来关闭呈现的视图
vc.restaurantSelectedCallBack = ^(RestaurantLocationData *restaurantValue) {
self.restaurantInfo = restaurantValue;
[[API sharedInstance] setRestaurantInfoCustom:restaurantValue];
[self dismissViewControllerAnimated:YES completion:nil];
};
vc.searchSelectedCallBack = ^(RestaurantLocationData *restaurantValue) {
self.restaurantInfo = restaurantValue;
[[API sharedInstance] setRestaurantInfoCustom:restaurantValue];
[self dismissViewControllerAnimated:NO completion:nil];
};
答案 0 :(得分:0)
如果您有此definesPresentationContext = true
->删除它或将其设置为 false
并确保在关闭ViewController之前添加此代码
if searchController.isActive == true {
// Set the search controller active to false to prevent crash
searchController.isActive = false
}