简体   繁体   English

将UINavigationBar添加到以模态方式呈现的TableViewController中?

[英]Add a UINavigationBar to a TableViewController presented modally?

Let's say that I'm trying to create something like the contacts app. 假设我正在尝试创建类似联系人应用的内容。 I have a TableViewController (TVC) where you select an item and see all the information about it. 我有一个TableViewController(TVC),您可以在其中选择一个项目并查看有关它的所有信息。 You can tap on edit and then a TVC with static cells is presented. 您可以点击编辑,然后会显示带有静态单元格的TVC。 This same TVC is also used to add a new item. 同样的TVC也用于添加新项目。

I currently have a TVC that I'm trying to re-use. 我目前有一台TVC,我正在尝试重复使用。 When the user is editing my model, I push the TVC into the navigation stack, but when the user is creating a new item I present the TVC modally. 当用户正在编辑我的模型时,我将TVC推入导航堆栈,但是当用户创建新项目时,我会以模态方式呈现TVC。

Obviously, when the TVC is pushed automatically it gets a NavigationBar. 显然,当自动推送TVC时,它会获得一个NavigationBar。 On the other hand, when I present it modally, I create my own NavigationBar (with buttons and everything else I need) and I add it as a subview. 另一方面,当我以模态方式呈现它时,我创建了自己的NavigationBar(带有按钮和我需要的其他所有东西)并将其添加为子视图。 This presents two problems: 这提出了两个问题:

  • The NavigationBar is on top of the first cell. NavigationBar位于第一个单元格的顶部。
  • The NavigationBar scrolls with the other cells. NavigationBar与其他单元格一起滚动。

For what I've read, this happens because I'm adding the NavigationBar to the TVC itself. 对于我所读到的,这是因为我将NavigationBar添加到TVC本身。
Having a TableView inside a Navigation controller sounded like an option, but without a TVC I can't have static cells on my TableView. 在导航控制器内部使用TableView听起来像一个选项,但没有TVC,我的TableView上不能有静态单元格。

I thought about not re-using my TVC, but I'd still be stuck with the same problem as I need a TVC with a bar on top. 我想不要重新使用我的TVC,但是我仍然遇到同样的问题,因为我需要一个顶部有条形电视的TVC。

How can I add a NavigationBar to a TVC with static cells when it is presented modally? 如果以模态方式呈现静态单元格,如何将一个NavigationBar添加到TVC? Is there another way to tackle this problem? 有没有其他方法可以解决这个问题?

By the way, I'm using storyboards and I'm targeting iOS6+, so I can't use presentModalViewController: 顺便说一句,我正在使用故事板,我的目标是iOS6 +,所以我不能使用presentModalViewController:

You can always create a UINavigationController with code, then set the existing table view controller as the root view controller. 您始终可以使用代码创建UINavigationController,然后将现有的表视图控制器设置为根视图控制器。 Then you can present the newly created UINavigationController modally. 然后,您可以以模态方式呈现新创建的UINavigationController。

With UINavigationController instance, you will have a UINavigationBar added. 使用UINavigationController实例,您将添加UINavigationBar。

Get the existing UITableViewController from your storyboard by using the method instantiateViewControllerWithIdentifier . 通过使用方法instantiateViewControllerWithIdentifier从故事板中获取现有的UITableViewController。 Do not forget to set the identifier in your storyboard first by setting the Storyboard ID. 不要忘记首先通过设置Storyboard ID在故事板中设置标识符。 For example, below I set the identifier as CategoriesViewController. 例如,下面我将标识符设置为CategoriesViewController。

设置故事板ID

Then I can obtain it with the following code: 然后我可以使用以下代码获取它:

UITableViewController *tableViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"CategoriesViewController"];
UINavigationController *navcon = [[UINavigationController alloc] initWithRootViewController:tableViewController];
[self presentViewController:navcon animated:YES completion:nil];

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

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