将 ToolBar 添加到 UITableView 的正确方法是什么?

发布于 2024-07-26 03:34:24 字数 385 浏览 5 评论 0原文

我正在编写一个基于导航的 iPhone 应用程序,我希望将 UIToolBar 停靠在屏幕底部,并在工具栏和导航栏之间滚动 UITableView。

我见过几个论坛,其中有人建议处理此视图的视图控制器应该是标准 UIViewController 而不是 UITableViewController。 除了所有标准 UIViewController 覆盖之外,视图控制器还必须实现 UITableView 委托和数据源方法。 除了上述协议之外,我还需要在此视图控制器子类中重新创建哪些(如果有)内置功能,以使其像 UITableViewController 一样工作? 走这条路我会失去什么吗?

或者将 UITableViewController 嵌套在标准 UIViewController 中会更好吗?

I'm writing a Navigation-Based iPhone app, and I'd like to have a UIToolBar docked at the bottom of my screen, with a UITableView scrolling between the tool bar and the navigation bar.

I've seen a couple of forums where it's been suggested that the View Controller handling this view should be a standard UIViewController rather than a UITableViewController. The view controller would have to implement the UITableView delegate and data source methods in addition to all of the standard UIViewController overrides. What (if any) built-in functionality do I need to recreate in this view controller subclass other than the aforementioned protocols to have it act like a UITableViewController? Is there anything I'm losing by going this route?

Or would it be better to nest a UITableViewController inside of a standard UIViewController?

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

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

发布评论

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

评论(5

只等公子 2024-08-02 03:34:24

从 OS 3.0 开始,导航控制器内置了一个工具栏。要使其显示:

[self.navigationController setToolbarHidden:NO];

通过在视图控制器中实现:

- (void)setToolbarItems:(NSArray *)toolbarItems animated:(BOOL)animated

,您可以配置工具栏的项目。

因此,您不必再担心工具栏位于层次结构中的位置。

(更正错字)

As of OS 3.0 the Navigation Controller has a tool bar built in. To make it appear:

[self.navigationController setToolbarHidden:NO];

By implmenting:

- (void)setToolbarItems:(NSArray *)toolbarItems animated:(BOOL)animated

in your view controller, you can configure the items of the tool bar.

So you no longer have to worry about where the tool bar is located in your hierarchy.

(corrected typo)

仅此而已 2024-08-02 03:34:24

Corey Floyd 大部分是正确的,除了

[self.navigationController setToolBarHidden:NO];

应该

[self.navigationController setToolbarHidden:NO];

是,即“setToolbarHidden”中的“b”必须是小写。 此外,方法名称列于 iPhone OS Reference 实际上

- (void)setToolbarHidden:(BOOL)hidden animated:(BOOL)animated

似乎省略 animated 参数也有效。

Corey Floyd is mostly correct, except that

[self.navigationController setToolBarHidden:NO];

should be

[self.navigationController setToolbarHidden:NO];

That is, the "b" in "setToolbarHidden" must be lowercase. Also, method name listed in the iPhone OS Reference is actually

- (void)setToolbarHidden:(BOOL)hidden animated:(BOOL)animated

though it seems that omitting the animated parameter works too.

终难愈 2024-08-02 03:34:24
//Tool bar
[self.navigationController setToolbarHidden:NO];

UIBarButtonItem *buttonItem = [[ UIBarButtonItem alloc ] initWithTitle: @"Select All"
                                                style: UIBarButtonItemStyleBordered
                                               target: self
                                               action: @selector(selectAll:) ];
UIBarButtonItem *buttonNext = [[UIBarButtonItem alloc]initWithTitle:@"Next" style:UIBarButtonItemStyleBordered target:self action:@selector(goNext:)];
self.toolbarItems = [ NSArray arrayWithObjects: buttonItem, buttonNext, nil ];

[ buttonItem release ];
[buttonNext release];
//Tool bar
[self.navigationController setToolbarHidden:NO];

UIBarButtonItem *buttonItem = [[ UIBarButtonItem alloc ] initWithTitle: @"Select All"
                                                style: UIBarButtonItemStyleBordered
                                               target: self
                                               action: @selector(selectAll:) ];
UIBarButtonItem *buttonNext = [[UIBarButtonItem alloc]initWithTitle:@"Next" style:UIBarButtonItemStyleBordered target:self action:@selector(goNext:)];
self.toolbarItems = [ NSArray arrayWithObjects: buttonItem, buttonNext, nil ];

[ buttonItem release ];
[buttonNext release];
执手闯天涯 2024-08-02 03:34:24

您所需要做的就是实现您所需的表视图功能级别所需的 UITableViewDelegate 和 UITableViewDatasource 方法。 这些方法可以位于任何类中,尽管所述类应符合相关协议。 应在 UITableView 实例上设置委托和数据源 - 通过编程方式或使用 Interface Builder。 根据文档,您失去一些功能 - 请参阅概述部分

就我个人而言,我发现许多开发人员似乎痴迷于在单个整体视图控制器类中提供所有这些功能,并且因为他们的视图中有一个表视图,所以必须使用 UITableViewController 的子类。 但是,我喜欢考虑单一职责原则,并且经常将数据源和委托分成单独的当复杂性非简单时的类。 该代码也不依赖于特定的 UIViewController 实现。

在我有单独的数据源/委托类的情况下,我经常使用 Interface Builder 而不是在代码中构建并将它们连接到表视图。 这种方法(至少对我来说)符合 依赖注入 的精神,并且节省了编写一些锅炉的时间-plate 代码,并提供一定程度的解耦

这些选择当然会受到您想要实现的功能复杂性的影响 - 对于简单的实现,我可能会发现自己使用 UITableViewController。

All you need do is implement the UITableViewDelegate and UITableViewDatasource methods required for the level of table view functionality you require. These methods can be in any class(es) though said classes should conform to the relevant protocols. The delegate and datasource should be set on the UITableView instance - either programatically or with Interface Builder. According to the docs you will lose some functionality - see the overview section.

Personally I find that many developers seem to be obsessed with providing all of this functionality in a single monolithic view controller class, and that because they have a table view in their view then a subclass of UITableViewController must be used. However, I like to consider the Single Responsibility Principle and will often break the datasource and delegate into separate classes when the complexity is anything other than simple. The code is also then not tied to a specific UIViewController implementation.

In situations where I have separate datasource/delegate classes I often construct and wire them up to the table view using Interface Builder and not in code. This approach (to me at least) is in the spirit of Dependency Injection and saves writing some boiler-plate code, and provides some level of decoupling.

These choices of course are influenced by the complexity of the functionality that you are trying to achieve - for simple implementations I might find myself using UITableViewController.

德意的啸 2024-08-02 03:34:24

试试这个:

self.navigationController.toolbarHidden = NO;

希望对您有帮助。

Try out this:

self.navigationController.toolbarHidden = NO;

Hope it helps you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文