使用UISearchController取消搜索会导致崩溃 [英] Cancelling search with UISearchController causes crash

查看:158
本文介绍了使用UISearchController取消搜索会导致崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的应用中,我们有一个具有UISearchController的UITableViewController:

In our app we have a UITableViewController that has a UISearchController:

searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
[self.searchController.searchBar sizeToFit];

self.tableView.tableHeaderView = self.searchController.searchBar;
self.showFooterView = YES;

self.searchController.delegate = self;
self.searchController.dimsBackgroundDuringPresentation = NO; // default is YES
self.searchController.searchBar.delegate = self;
self.definesPresentationContext = YES;

表视图控制器也是UISearchBarDelegate 和UISearchControllerDelegate.

The table view controller is also a UISearchBarDelegate and UISearchControllerDelegate.

#pragma mark - UISearchBarDelegate
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [searchBar resignFirstResponder];
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    self.contacts = self.allContacts;
    [self.tableView reloadData];
}

现在一切正常,但是有时用户开始搜索,在搜索栏中键入几个字符,返回结果,然后用户取消搜索,然后发生这种情况:

Now everything works as expected, but there are occasions when a user starts a search, types in a few characters in the search bar, results are returned, and the user cancels the search and then this happens:

Fatal Exception: NSInvalidArgumentException
-[_UIFullscreenPresentationController adaptivePresentationController]: unrecognized selector sent to instance 0x147c81ce0

Fatal Exception: NSInvalidArgumentException
0  CoreFoundation                 0x1842b4f48 __exceptionPreprocess
1  libobjc.A.dylib                0x198d77f80 objc_exception_throw
2  CoreFoundation                 0x1842bbc5c __methodDescriptionForSelector
3  CoreFoundation                 0x1842b8c00 ___forwarding___
4  CoreFoundation                 0x1841bccac _CF_forwarding_prep_0
5  UIKit                          0x18a1ba084 -[UISearchController _searchPresentationController]
6  UIKit                          0x189e7d10c -[_UISearchControllerTransplantSearchBarAnimator animateTransition:]
7  UIKit                          0x189b9fa90 __56-[UIPresentationController runTransitionForCurrentState]_block_invoke
8  UIKit                          0x189af856c _runAfterCACommitDeferredBlocks
9  UIKit                          0x189b054bc _cleanUpAfterCAFlushAndRunDeferredBlocks
10 UIKit                          0x189839984 _afterCACommitHandler
11 CoreFoundation                 0x18426bbd0 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__
12 CoreFoundation                 0x184269974 __CFRunLoopDoObservers
13 CoreFoundation                 0x184269da4 __CFRunLoopRun
14 CoreFoundation                 0x184198ca0 CFRunLoopRunSpecific
15 GraphicsServices               0x18f3d4088 GSEventRunModal
16 UIKit                          0x1898b0ffc UIApplicationMain

尽管Fabric在生产中报告了此错误,但我们从未能够重现此错误.

We have never been able to reproduce this error, although it gets reported by Fabric on production.

此问题类似于此问题:

This problem looks similar to this: Selecting cell after search doesn't segue visually, but loads next view Swift Xcode but no real answer has been given there yet.

我已经开始研究表示控制器,但是我们没有特殊功能需要以特定方式设置表示控制器.

I have started looking into presentation controllers but we have no special functionality that would require setting up presentation controllers in a specific way.

关于如何解决此问题的任何想法?

Any ideas on how to fix this?

谢谢

推荐答案

我在Swift上也遇到了同样的问题.

I've faced the same problem on Swift.

问题是Searchbarcontroller仍然保留ViewController的引用(委托).

The problem is Searchbarcontroller still hold the reference(delegate) of your ViewController.

因此,您要做的就是在视图取消分配或消失时手动删除引用

So all you have to do is manually remove the reference when view dealloc or disappear

类似这样的东西:

- (void)dealloc {
    self.searchController.searchResultsUpdater = nil;
    self.searchController.searchBar.delegate = nil;
    self.searchController.delegate = nil;
    self.searchController = nil;
}

这篇关于使用UISearchController取消搜索会导致崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆