【问题标题】:UISearchController issues when dismissing modally模态关闭时的 UISearchController 问题
【发布时间】:2016-05-24 05:03:10
【问题描述】:

单击导航栏按钮时,我以模态方式呈现视图。这个视图有一个 tableview 视图。单击 tableview 行时,视图将被关闭。添加 UISearchController 时遇到问题。当我从搜索栏的结果中单击一个项目时,模态消失,但在它后面是相同的确切视图,但 tableview 显示的是一个完全填充的视图。一秒钟后,该视图以模态方式消失,我又回到了导航视图。我包含了一个 gif,以帮助直观地解释我的问题。这是代码sn-ps。

在带有 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];
};

【问题讨论】:

  • 在dismiss viewcontroller之前或之后试试这行代码[self.searchDisplayController setActive:NO animated:YES];
  • 自 iOS 8 起已弃用
  • searchController.active = false 或关闭 searchController,因为对我来说两者都在工作。
  • 谢谢@JP_Mob ,searchController.active = false 在 vi​​ewDidDisappear 上帮助了我

标签: ios objective-c uitableview uisearchcontroller


【解决方案1】:

如果你有这个definesPresentationContext = true -> 删除它或将它设置为 false

并确保在关闭 ViewController 之前添加此代码

if searchController.isActive == true { // Set the search controller active to false to prevent crash searchController.isActive = false }

【讨论】:

  • 谢谢,即使没有禁用definesPresentationContext,它也帮助了我,我拥有它等于true
猜你喜欢
  • 2019-01-30
  • 2018-04-08
  • 2017-03-22
  • 2017-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-05
相关资源
最近更新 更多