将工具栏添加到UITableViewController [英] Add toolbar to UITableViewController

查看:250
本文介绍了将工具栏添加到UITableViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将UIToolBar添加到UITableViewController的最简单的方法是什么?

What is the simplest way to add UIToolBar to UITableViewController? I'm depending on edit functionality, so I can't change UITableViewController to UIViewController easily.

推荐答案

根本没有问题,

为了使其工作,您必须:

In order for this to work you must:


  • 请确保您使用 UINavigationController 包含所有需要工具栏的视图控制器。

  • 设置需要工具栏的视图控制器的 toolbarsItems 属性。

  • Make sure that you use a UINavigationController to contain all your view controllers that needs a toolbar.
  • Set the toolbarsItems property of the view controller that wants a toolbar.

几乎和设置视图控制器的标题一样简单,并且应该以同样的方式。很可能通过覆盖 initWithNibName:bundle:初始化。例如:

This is almost as easy as as setting the view controller's title, and should be done the same way. Most probably by overriding the initWithNibName:bundle: initializer. As an example:

-(id)initWithNibName:(NSString*)name bundle:(NSBundle*)bundle;
{
  self = [super initWithNibName:name bundle:bundle];
  if (self) {
    self.title = @"My Title";
    NSArray* toolbarItems = [NSArray arrayWithObjects:
        [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd 
                                                      target:self
                                                      action:@selector(addStuff:)],
        [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch 
                                                      target:self
                                                      action:@selector(searchStuff:)],
        nil];
    [toolbarItems makeObjectsPerformSelector:@selector(release)];
    self.toolbarItems = toolbarItems;
    self.navigationController.toolbarHidden = NO;
  }
  return self;
}



您也可以使用 setToolbarItems:animated: code>而不是分配到 toolbarItems 属性,以动态方式添加和删除工具栏项。

You can also use setToolbarItems:animated: instead of assigning to the toolbarItems property, to add and remove toolbar items in an animated fashion on the fly.

这篇关于将工具栏添加到UITableViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆