如何在 Interface Builder 中向 UITableViewController 添加导航栏?
界面生成器不允许我单击导航栏并将其拖动到表视图控制器上!这太令人沮丧了。
我想要的只是一个带有编辑按钮的表格视图(在界面生成器中完成)。如果这是不可能的,那么如何以编程方式添加导航栏?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这是另一种简单方式;
您可以轻松添加导航栏。
This is the other easy way ;
You can add navigation bar easily.
从大纲视图中,确保选择了表视图控制器。
然后转到编辑器菜单,单击嵌入子菜单,然后选择导航控制器,瞧。您的导航控制器指向具有内置关系的表格视图控制器。
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.
对于顶部有编辑按钮的表视图,请使用 UINavigationController,并将 UITableView 作为 rootView。这意味着您将为表视图创建一个自定义 UITableView 子类,并将其用作 UINavigationController 实例的 rootView。 (以编程方式,它是通过 UINavigationController 的
-(id)initWithRootViewController
设置的。它也可以通过 IB 设置。)然后,在您的 UITableView 子类中,取消注释以下行:
瞧,您的 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:
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.我同意很难弄清楚如何在 Interface Builder 中执行此类操作,但幸运的是,可以通过这种方式将导航栏和栏按钮项添加到表视图中。操作方法如下:
编辑
上述方法的问题在于,正如 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:
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 ofUITableView
resized to fit. Unfortunately, that means you would have to implement theUITableViewController
behavior needed by yourUIViewController
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 ofUIView
) that fits under the Navigation Bar. Your custom subclass would have an outlet pointing to an instance ofUITableViewController
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 inviewDidLoad
.The parent could implement the action methods for the Navigation Bar's button items, and implement the delegate pattern if necessary.
从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.
首先添加一个导航控制器,并将表视图控制器(作为根视图控制器)放到导航控制器上。这是在代码中完成的,因为我不使用 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.
为什么你不能将一个 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.
这些步骤在 iOS 9 中对我有用:
These steps worked for me in iOS 9: