简体   繁体   English

如何将NavigationBar(没有NavigationController)和SearchBar添加到TableView

[英]How to add NavigationBar (without NavigationController) and SearchBar to a TableView

I get the feeling this really shouldn't be giving me as much trouble as it is and that I'm missing something, but I just can't seem to get this to work. 我觉得这真的不应该给我带来的麻烦和我错过的东西,但我似乎无法让它发挥作用。 So I want a TabBar that switches between 3 different TableViews. 所以我想要一个在3个不同的TableView之间切换的TabBar。 All of them need to have a NavigationBar but not a NavigationController (as it's all taking place after an initial modal segue into the TabBarController). 所有这些都需要一个NavigationBar而不是一个NavigationController(因为它都是在初始模态进入TabBarController之后发生的)。 the perfect example of what I'm after is the Apple Maps bookmarks. 我所追求的完美例子是Apple Maps书签。

I've tried three different methods all of which have failed in different ways: 我尝试了三种不同的方法,所有这些方法都以不同的方式失败了:

  1. Add a TableView, then add a NavBar to this, then add the SearchBar. 添加一个TableView,然后添加一个NavBar,然后添加SearchBar。 The SearchBar simply replaces the NavBar. SearchBar只是替换了NavBar。
  2. Add a View, then add the NavBar to this. 添加一个视图,然后将NavBar添加到此。 Then add the TableView as a subView. 然后将TableView添加为subView。 When I add the SearchBar to this subView it ends up underneath the prototype cell (didn't try running this as I would have to write a new ViewController to handle the fact that the TableView is now a subview and didn't think it was worth it just yet given the layout problems). 当我将SearchBar添加到这个子视图时,它最终会在原型单元格下面运行(没有尝试运行它,因为我必须编写一个新的ViewController来处理TableView现在是一个子视图并且不认为值得的事实它只是给出了布局问题)。
  3. Add a View, then add a NavBar to this. 添加一个视图,然后添加一个NavBar。 then add a ContainerView which has a TableViewController. 然后添加一个具有TableViewController的ContainerView。 This simply crashes on running. 这只是在跑步时崩溃了。 I'm not sure what the error is here as I'm not really too sure where to catch any exceptions... 我不确定这里的错误是什么,因为我不太确定在哪里可以捕获任何异常...

I've implemented most of my interface in the IB so far and would like to keep it that way as much as possible, but I'm sure there must be something obvious I'm missing here. 到目前为止,我已经在IB中实现了我的大部分界面,并且希望尽可能地保持这种方式,但我确信必须有一些明显我在这里缺失的东西。 I've not been iOS programming for very long so go easy on me if I'm missing something fundamental! 我已经很长时间没有进行过iOS编程了,所以如果我错过了一些基本的东西,那就轻松吧! Any help would be appreciated greatly. 任何帮助将非常感激。 Even if it's just saying which above approach I should go with and keep trying at. 即使它只是说我应该采用哪种方法并继续尝试。 Thanks! 谢谢!

Edit 编辑

I should probably mention that when I said TableViewController I meant a custom subclass of a TableViewController which uses a FetchedResultsController to populate the cells. 我应该提一下,当我说TableViewController时,我的意思是TableViewController的自定义子类,它使用FetchedResultsController来填充单元格。

If you're going to have a modal segue to a new view controller (or a tab bar controller with tabs that point to navigation controllers), and you want that view controller to have a navigation bar, then you should select the view controller you want to present modally, embed it in a navigation controller (Editor->Embed In->Navigation Controller), and then modally segue to the navigation controller. 如果您要对新的视图控制器(或带有指向导航控制器的选项卡的标签栏控制器)进行模态转换,并且您希望该视图控制器具有导航栏,那么您应该选择视图控制器想要以模态方式呈现,将其嵌入导航控制器(Editor-> Embed In-> Navigation Controller),然后以模态方式转换到导航控制器。 Same concept applies for your tab bar controller except they will be relationship segues. 相同的概念适用于您的标签栏控制器,除了它们将是关系segues。 带导航栏的模态segue的图像

You should add a navigation bar to a UIViewController (not a table view controller), then add the table view, and to that table view add a search bar and a cell. 您应该向UIViewController(而不是表视图控制器)添加导航栏,然后添加表视图,并向该表视图添加搜索栏和单元格。 I've tested this, and it works fine. 我测试了这个,它工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM