【发布时间】:2016-04-15 07:18:43
【问题描述】:
以前有人问过这个问题,但我无法清楚地解释这个问题。我有一个带有表格视图和搜索功能的简单应用程序。在viewDidLoad() 中,我调用setUpSearchView(),其定义如下
let searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.hidesNavigationBarDuringPresentation = true
searchController.dimsBackgroundDuringPresentation = false
self.tableView.tableHeaderView = searchController.searchBar
此代码无法正常工作。在控制台上,我可以看到这个错误
Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior
此外,当我在搜索栏上键入内容时,searchcontroller 没有正确设置动画并且没有调用updateSearchResultsForSearchController delegate。
但是,所有这些问题都可以通过对 searchController 初始化函数进行微小的更改来轻松解决。与其将 searchController 声明为函数中的局部变量,不如将其声明为像 var searchController:UISearchController! 这样的方法外部的实例变量,一切正常,尽管我不知道为什么。
现在代码看起来像这样
var searchController:UISearchController!
在 setUpSearchView() 中
searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.hidesNavigationBarDuringPresentation = true
searchController.dimsBackgroundDuringPresentation = false
self.tableView.tableHeaderView = searchController.searchBar
这里是github上viewController的链接:https://github.com/tmsbn/marvel_heroes/blob/master/avengers/HeroesListController.swift
有人能解释为什么会这样吗?这是swift的错误吗?或者是 iOS 中我不知道的东西。
【问题讨论】:
标签: ios swift uisearchcontroller