整个应用程序的 UISearchBar 相同吗? [英] Same UISearchBar for entire app?
问题描述
我见过很多应用都有一个始终位于应用顶部的通用搜索栏.我在一个视图控制器中实现了一个带有 UITableView
的 UISearchBar
.我想在我的应用程序中的其他视图控制器上有相同的搜索栏.我如何将这些其他 UISearchBars
链接到我已经创建的那个?即,我如何配置这些其他 UISearchBars
以便它们返回相同的搜索结果并链接到相同的 UITableView
?
I've seen a lot of apps that have a universal search bar that always remains at the top of the app. I have implemented a UISearchBar
with a UITableView
in one view controller. I want to have the same search bar on other view controllers in my app. How do I link these other UISearchBars
to the one I have already created? I.e., how do I configure these other UISearchBars
so that they return the same search results and link to the same UITableView
?
推荐答案
嵌套视图控制器可能正是您所需要的.
Nested view controllers may be what you need.
定义一个顶视图控制器",它管理一个包含搜索栏的顶视图"并将您的(表格)视图添加到顶视图(在顶视图上使用 -addSubview:
)和关联的视图控制器到顶部视图控制器(在顶部 VC 中使用 -addChildViewController:
及其本身).
Define a "top view controller" that manages a "top view" containing a search bar and add your (table) views to the top view (using -addSubview:
on the top view) and the associated view controller(s) to the top view controller (using -addChildViewController:
in the top VC and on itself).
在 Interface Builder 中,您可以定义一个顶视图,并在其中定义一个容器视图".然后系统处理子视图和子视图控制器的插入.
In Interface Builder, you can define a top view and inside it a "container view." The system then handles the insertion of the subview and sub-view controller.
通过定义良好的视图控制器层次结构,您可以使您的应用程序设计更加合乎逻辑和简洁.我建议在深入编码之前花一些时间研究一个好的层次结构.
By defining a good view controller hierarchy, you make your app design more logical and clean. I’d recommend to take some time into investigating a good hierarchy before diving into coding.
最后一点:UISearchDisplayController
是一个对象(显然不是视图控制器),它在视图控制器的视图上方叠加一个搜索栏.您可以简单地将它应用到最顶层的视图控制器(始终可见的视图控制器,如导航控制器)上方.值得研究一下,如果你还没有的话.;-)
A final note: the UISearchDisplayController
is an object (apparently not a view controller) that superimposes a search bar above a view controller’s view. You might be able to simply apply it immediately above the top-most view controller (the one that is always visible, like a navigation controller). It’s worth looking into it, if you didn’t already. ;-)
XYZTopViewController
(管理一个XYZTopView
)UINavigationController
(管理由 Apple 定义的私有导航视图层次结构)XYZFirstPageViewController
(管理XYZFirstPageView
)(根"视图控制器)XYZSecondPageViewController
(管理XYZSecondPageView
)(在需要时由导航控制器推送)
XYZTopViewController
(managing aXYZTopView
)UINavigationController
(managing a private navigation view hierarchy defined by Apple)XYZFirstPageViewController
(managing aXYZFirstPageView
) (the "root" view controller)XYZSecondPageViewController
(managing aXYZSecondPageView
) (pushed by nav. controller when you need it to)
XYZTopView
UISearchBar
- (Apple 定义的私有导航视图层次结构)
XYZFirstPageView
- (属于第一页/屏幕的视图层次结构)
这篇关于整个应用程序的 UISearchBar 相同吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!