ios – 将UISearchController与UINavigationController一起使用

ios – 将UISearchController与UINavigationController一起使用,第1张

概述我遇到的问题与 here相同,没有任何答案. (将searchController.active设置为false会清除搜索文本,这是我不想要的.)我想使用UISearchController,以便用户可以搜索我的UITableView中的项目列表.我在Interface Builder中连接了一个show segue,它在选择表视图的单元格时触发.  问题是,如果用户搜索某些内容然后单击该表格的单 我遇到的问题与 here相同,没有任何答案. (将searchController.active设置为false会清除搜索文本,这是我不想要的.)我想使用UISearchController,以便用户可以搜索我的UItableVIEw中的项目列表.我在Interface Builder中连接了一个show segue,它在选择表视图的单元格时触发.
 问题是,如果用户搜索某些内容然后单击该表格的单元格,当它转移到新视图时,搜索栏就会位于该位置.理想情况下,我希望在用户搜索时导航栏被搜索栏“替换”,然后在用户点击单元格时返回导航栏,然后在用户单击时返回搜索栏“返回键. (这是现在已弃用的UISearchdisplayController的工作方式.)如何实现这一目标?这是我的表视图的控制器.

class ItemSearchVIEwController: UItableVIEwController,UISearchResultsUpdating{    var searchController: UISearchController?    let itemList = [ItemList(category:"Chocolate",name:"chocolate bar",price: 1234),ItemList(category:"Chocolate",name:"chocolate Chip",name:"dark chocolate",ItemList(category:"Hard",name:"lollipop",name:"candy cane",name:"jaw breaker",ItemList(category:"Other",name:"caramel",name:"sour chew",name:"gummi bear",price: 1234)]    var filteredList : [ItemList] = []    overrIDe func vIEwDIDLoad()    {        super.vIEwDIDLoad()        self.Title = "Item Search"        self.tableVIEw.delegate = self        self.tableVIEw.dataSource = self        self.searchController = UISearchController(searchResultsController: nil)        self.searchController!.searchResultsUpdater = self        self.searchController!.hIDesNavigationbarDuringPresentation = true        self.searchController!.dimsBackgroundDuringPresentation = false        self.searchController!.searchbar.searchbarStyle = .Minimal        self.searchController!.searchbar.sizetoFit()        self.tableVIEw.tableheaderVIEw = self.searchController!.searchbar      }    overrIDe func prepareForSegue(segue: UIStoryboardSegue,sender: AnyObject?)    {        if (segue.IDentifIEr == "itemDetail")        {            let itemDetailVIEwController = segue.destinationVIEwController as UIVIEwController            let selectedCell = sender as UItableVIEwCell            itemDetailVIEwController.Title = selectedCell.textLabel?.text        }    }    overrIDe func tableVIEw(tableVIEw: UItableVIEw,cellForRowAtIndexPath indexPath: NSIndexPath) -> UItableVIEwCell    {        let cell = self.tableVIEw.dequeueReusableCellWithIDentifIEr("Cell") as UItableVIEwCell        var item : ItemList        if self.searchController!.active        {            item = self.filteredList[indexPath.row]        }        else        {            item = self.itemList[indexPath.row]        }        cell.textLabel!.text = item.name        return cell}    overrIDe func tableVIEw(tableVIEw: UItableVIEw,numberOfRowsInSection section: Int) -> Int    {        if (self.searchController!.active)        {            return self.filteredList.count        }        else        {            return self.itemList.count        }    }}extension ItemSearchVIEwController: UISearchResultsUpdating{    func updateSearchResultsForSearchController(searchController: UISearchController)    {        if (searchController.searchbar.text.isEmpty)        {            self.filteredList = self.itemList        }        else        {            let searchPredicate =            {                (item: ItemList) -> Bool in                item.name.rangeOfString(searchController.searchbar.text,options: .CaseInsensitiveSearch) != nil            }            self.filteredList = self.itemList.filter(searchPredicate)        }        self.tableVIEw.reloadData()    }}
解决方法 在vIEwDIDLoad()中添加此行

definesPresentationContext = true

来自definesPresentationContext的文档

A Boolean value that indicates whether this vIEw controller’s vIEw is covered when the vIEw controller or one of its descendants presents a vIEw controller.

讨论

When a vIEw controller is presented,iOS starts with the presenting vIEw controller and asks it if it wants to provIDe the presentation
context. If the presenting vIEw controller does not provIDe a context,
then iOS asks the presenting vIEw controller’s parent vIEw
controller. iOS searches up through the vIEw controller hIErarchy
until a vIEw controller provIDes a presentation context. If no vIEw
controller offers to provIDe a context,the window’s root vIEw
controller provIDes the presentation context.

If a vIEw controller returns true,then it provIDes a presentation context. The portion of the window covered by the vIEw controller’s vIEw determines the size of the presented vIEw controller’s vIEw. The default value for this property is false.

总结

以上是内存溢出为你收集整理的ios – 将UISearchController与UINavigationController一起使用全部内容,希望文章能够帮你解决ios – 将UISearchController与UINavigationController一起使用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1045003.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存