菜鸟教程小白 发表于 2022-12-11 17:12:54

ios - 为什么 iOS viewPager 与 UISearchController 崩溃?


                                            <p><p>现在我在 ios 应用程序开发过程中遇到了一个问题。
我做了一个viewpagercontrollerview 包括几个viewcontroller。
每个 Viewcontroller 都包含 tableview。
我在这个 tableview 中使用了 UISearchController。
但是,每当我单击 searchcontroller 并滚动选项卡时,就会发生错误。</p>

<pre><code> 2016-08-08 22:07:11.211 ToolManager *** Assertion failure in -, /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3512.60.7/_UIQueuingScrollView.m:332
2016-08-08 22:07:11.222 ToolManager *** Terminating app due to uncaught exception &#39;NSInternalInconsistencyException&#39;, reason: &#39;Unexpected subviews&#39;
*** First throw call stack:
(
0   CoreFoundation                      0x0000000104c3dd85 __exceptionPreprocess + 165
1   libobjc.A.dylib                     0x00000001046b1deb objc_exception_throw + 48
2   CoreFoundation                      0x0000000104c3dbea + + 106
3   Foundation                        0x00000001042fbd5a - + 198
</code></pre>

<p>但是,没有点击搜索控件和滚动,那么工作做得很好。
我看到了几个分辨率...
一种方法是在 scool 选项卡之前关闭 searchcontroller。
但是我怎样才能捕获标签滚动!
现在我在每个 ViewController 中添加 searchcontroll。</p>

<pre><code>@interface CurrentToolListViewController : BaseViewController&lt;UITableViewDelegate, UITableViewDataSource,
                UISearchResultsUpdating, UISearchControllerDelegate&gt;


@property (nonatomic, strong) UISearchController *searchController;
@property (nonatomic, copy) NSString *filterString;


@end
</code></pre>

<p>和.m文件</p>

<pre><code>self.searchController = [ initWithSearchResultsController:nil];

self.searchController.searchResultsUpdater = self;

self.searchController.dimsBackgroundDuringPresentation = NO;

self.searchController.delegate = self;

;

self.tblCurToolList.tableHeaderView = self.searchController.searchBar;

self.definesPresentationContext = YES;
</code></pre>

<p>请帮帮我。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您必须在 <strong>viewWillDisappear(_:)</strong> 方法中使 searchController 处于非事件状态</p>

<pre><code>-(void) viewWillDisappear:(BOOL)animated {
    ;

    self.searchController.active = NO;

}
</code></pre>

<p><strong>编辑</strong></p>

<p>如果您<em>滑动</em> 到下一个 ViewController,但如果您使用 <code>UIButton</code> 调用 <code>setViewController</code>,上述代码应该可以修复您的应用程序崩溃>,那么您必须检查 <code>UIButton</code> 的 <code>@IBAction</code> 中的 searchController 是否处于事件状态(使用 if 语句)。在 if 语句中,键入 <code>self.searchController.active = NO;</code>,然后键入 <code>return</code>。</p>

<p>这只是让 searchController 处于非事件状态,然后你必须再次按下按钮以使其调用适当的方法(<code>setViewController</code>)</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 为什么 iOS viewPager 与 UISearchController 崩溃?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/38842861/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/38842861/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 为什么 iOS viewPager 与 UISearchController 崩溃?