如何在 Interface Builder 中向 UITableViewController 添加导航栏?

发布于 2024-10-18 13:39:21 字数 106 浏览 7 评论 0原文

界面生成器不允许我单击导航栏并将其拖动到表视图控制器上!这太令人沮丧了。

我想要的只是一个带有编辑按钮的表格视图(在界面生成器中完成)。如果这是不可能的,那么如何以编程方式添加导航栏?

Interface builder does not let me click and drag a Navigation Bar onto a Table View Controller!!! It is super frustrating.

All I want is a table view with an edit button (done in interface-builder). If this is not possible, then how do I add a navbar progammatically?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

梦幻的心爱 2024-10-25 13:39:22

这是另一种简单方式;

  1. 在情节提要上选择 TableViewController 屏幕。
  2. 单击右侧菜单上的尺寸检查器符号,找到模拟尺寸
  3. 将固定更改为自由形式

您可以轻松添加导航栏。

This is the other easy way ;

  1. Choose your TableViewController screen on storyboard.
  2. Click Size Inspector symbol on the right menu and find Simulated Size
  3. Change Fixed to Free Form

You can add navigation bar easily.

焚却相思 2024-10-25 13:39:21

从大纲视图中,确保选择了表视图控制器

然后转到编辑器菜单,单击嵌入子菜单,然后选择导航控制器,瞧。您的导航控制器指向具有内置关系的表格视图控制器。

From the outline view, make sure your Table View Controller is selected.

Then go to the Editor menu, and click on the Embed In submenu, and choose Navigation Controller and voila. You have your navigation controller pointing to your tableview controller with a relationship built in.

倾听心声的旋律 2024-10-25 13:39:21

对于顶部有编辑按钮的表视图,请使用 UINavigationController,并将 UITableView 作为 rootView。这意味着您将为表视图创建一个自定义 UITableView 子类,并将其用作 UINavigationController 实例的 rootView。 (以编程方式,它是通过 UINavigationController 的 -(id)initWithRootViewController 设置的。它也可以通过 IB 设置。)

然后,在您的 UITableView 子类中,取消注释以下行:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

瞧,您的 UINavigationController 的视图显示为表格使用导航栏右侧的编辑按钮查看。

由于控制器位于堆栈的顶部,因此左侧没有“后退”按钮,因此您可以对您创建的任何 UIBarButtonItem 使用 self.navigationItem.leftBarButtonItem 。

For a table view with an edit button at the top, use a UINavigationController, with a UITableView as the rootView. That means you're going to make a custom UITableView subclass for your table view, and use that as the rootView of your UINavigationController instance. (Programatically, it's set with UINavigationController's -(id)initWithRootViewController. It's also settable through IB.)

Then, in your UITableView subclass, uncomment the following line:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

and voilà, your UINavigationController's view shows up as a table view with an edit button on the right side of the navigation bar.

Since the controller is at the top of the stack, there's no "back" button on the left, so you can use self.navigationItem.leftBarButtonItem for whatever UIBarButtonItem you create.

空宴 2024-10-25 13:39:21

我同意很难弄清楚如何在 Interface Builder 中执行此类操作,但幸运的是,可以通过这种方式将导航栏和栏按钮项添加到表视图中。操作方法如下:

  1. 将一个空白视图(UIView 的一个实例)从库拖到靠近表视图顶部的区域。当您在目标区域附近拖动时,Interface Builder 将以蓝色突出显示该区域,以向您显示放置视图的位置。放开,视图将被添加为表视图标题视图的子视图。
  2. 从库中拖动导航栏并将其放在刚刚添加的空白视图上。
  3. 从库中拖动一个栏按钮项目并将其放到导航栏上。

编辑

上述方法的问题在于,正如 Bogatyr 指出的那样,导航栏将随表视图一起滚动。 Apple 建议使用 UIViewController 的自定义子类,该子类同时拥有导航栏和调整大小以适应的 UITableView 实例。不幸的是,这意味着您必须自己实现 UIViewController 子类所需的 UITableViewController 行为。

另一种似乎效果很好的方法是创建一个 UIViewController 的自定义子类,它拥有一个包含导航栏的空白背景视图以及一个空白内容视图(UIView 的实例) >) 位于导航栏下方。您的自定义子类将有一个出口指向同一 nib 文件中的 UITableViewController 实例。

这样做的优点是允许在 Interface Builder 中创建和配置所有视图组件,并且不需要从头开始实现 UITableViewController 方法。在表视图控制器的父级中,您需要注意的唯一细节是将表视图添加为 viewDidLoad 中父级内容视图的子视图。

父级可以实现导航栏按钮项的操作方法,并在必要时实现委托模式。

I agree that it's difficult to figure out how to do things like this in Interface Builder, but luckily it is possible to add a Navigation Bar and Bar Button Item to a Table View this way. Here's how to do it:

  1. Drag a blank View (an instance of UIView) from the Library to the area near the top of the Table View. As you drag near the target area, Interface Builder will highlight it in blue to show you where to drop the View. Let go, and the View will be added as a subview of the Table View's header view.
  2. Drag a Navigation Bar from the Library and drop it on the blank View you just added.
  3. Drag a Bar Button Item from the Library and drop it onto the Navigation Bar.

EDIT

The problem with the above approach is that, as Bogatyr points out, the Navigation Bar will then scroll along with the Table View. Apple recommends using a custom subclass of UIViewController that owns both the Navigation Bar and an instance of UITableView resized to fit. Unfortunately, that means you would have to implement the UITableViewController behavior needed by your UIViewController subclass yourself.

Another approach that seems to work well is to create a custom subclass of UIViewController that owns a blank background view containing the Navigation Bar as well as a blank content view (an instance of UIView) that fits under the Navigation Bar. Your custom subclass would have an outlet pointing to an instance of UITableViewController in the same nib file.

This has the advantage of allowing all the view components to be created and configured in Interface Builder, and doesn't require implementing UITableViewController methods from scratch. The only detail you'd need to take care of in the Table View Controller's parent would be to add Table View as a subview of the parent's content view in viewDidLoad.

The parent could implement the action methods for the Navigation Bar's button items, and implement the delegate pattern if necessary.

掀纱窥君容 2024-10-25 13:39:21

从iOS6开始,你可以使用容器视图。因此,您要做的就是获取视图控制器,向其添加导航栏,然后将容器视图添加到同一视图控制器。它会自动将新的视图控制器链接添加到您的容器视图中。现在只需删除它以及故事板中的表视图控制器即可。现在通过控件拖动将表视图控制器嵌入到容器视图中。希望有帮助。

From iOS6 onwards, you can use container view. So what you have to do is take View controller, add the navigation bar to it, then add a Container View to same view controller. It will automatically, add the new view controller link to your container view. Now simply delete that, and your table view controller in the story board. Now embed the table view controller to container view by control drag. Hope it helps.

甚是思念 2024-10-25 13:39:21

首先添加一个导航控制器,并将表视图控制器(作为根视图控制器)放到导航控制器上。这是在代码中完成的,因为我不使用 IB。

First add a navigation controller and put the table view controller (as root view controller) onto the navigation controller. This is how it is done in Code because I don't use IB.

べ繥欢鉨o。 2024-10-25 13:39:21

为什么你不能将一个 navigationItem 拖到一个 .xib 文件中,并将 File's Owner 设置为 UIViewController 的子类,并将 navigationItem 连接到 UIViewController 的 navigationItem 出口,这超出了我的理解范围。这似乎是 IB / XCode 集成中的一个真正的漏洞。因为您当然可以将 ViewController 的实例拖到 xib 文件中,并将 navigationItem 拖到 ViewController 中,然后以这种方式设置标题和 barbuttonitems。

因此,如果您想在 IB 中定义 UITableViewController 子类对象的导航栏,则必须在 xib 文件中创建 TableVC 对象(但不是包含 UITableViewController 的表视图的 .xib 文件!)。然后,您可以将 TableVC 对象挂接到另一个对象(例如您的应用程序委托)的出口,如果您在应用程序的整个生命周期中只需要一个 TVC 实例,或者如果您想动态创建您的 TVC 实例,则该方法可以工作。 TableVC 在代码中通过 NSBundle 类的 loadNibNamed:owner:options 方法手动加载这个额外的 .xib 文件。

Why in the world you can't drag a navigationItem into a .xib file with File's Owner set to a subclass of UIViewController and hook the navigationItem up to the UIViewController's navigationItem outlet is beyond me. It seems like a real hole in IB / XCode integration. Because you can certainly drag an instance of ViewController to a xib file, and drag a navigationItem into the ViewController, and then set the title and barbuttonitems that way.

So if you want to define your UITableViewController subclass object's navigation bar in IB, you have to create your TableVC object in a xib file (not the one .xib file that contains the tableview for your UITableViewController, though!). You then either hook the TableVC object up to be an outlet of another object (like your application delegate), which works if you need just one instance of your TVC throughout the lifetime of your app, or if you want to dynamically create instances of your TableVC in code you load this extra .xib file manually via loadNibNamed:owner:options method of the NSBundle class.

分開簡單 2024-10-25 13:39:21

这些步骤在 iOS 9 中对我有用:

  1. 将视图控制器添加到 Storyboard。将 UITableViewController 作为基类。
  2. 将导航栏对象添加到顶部的视图控制器上。
  3. 在导航栏下方添加表视图。
  4. 将表视图单元添加到表视图中。
  5. 添加约束。

These steps worked for me in iOS 9:

  1. Add a View Controller to the Storyboard. Make UITableViewController as base Class.
  2. Add a Navigation Bar object onto view controller at the top.
  3. Add a Table View below Navigation bar.
  4. Add a Table View Cell into Table View.
  5. Add constraints.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文