如何以编程方式将UIToolbar添加到UITableViewController? [英] How to add a UIToolbar to a UITableViewController programmatically?

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

问题描述

我选择在没有笔尖的情况下使用 UITableViewController 。我需要一个底部带有两个按钮的UIToolbar。最简单的方法是什么?

I have opted to use a UITableViewController without a nib. I need a UIToolbar at the bottom with two buttons. What is the simplest way of doing that?

P.S。我知道我可以轻松地使用 UIViewController 并添加 UITableView 但是我希望整个应用程序看起来一致。

P.S. I know that I can easily use a UIViewController and add a UITableView however I want things to look consistent across the app.

有人可以帮忙吗?

我看到以下示例,我不确定其有效性:

I saw the following example and I am not sure on its validity:

(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    //Initialize the toolbar 
    toolbar = [[UIToolbar alloc] init]; toolbar.barStyle = UIBarStyleDefault;

    //Set the toolbar to fit the width of the app. 
    [toolbar sizeToFit];

    //Caclulate the height of the toolbar 
    CGFloat toolbarHeight = [toolbar frame].size.height;

    //Get the bounds of the parent view 
    CGRect rootViewBounds = self.parentViewController.view.bounds;

    //Get the height of the parent view. 
    CGFloat rootViewHeight = CGRectGetHeight(rootViewBounds);

    //Get the width of the parent view, 
    CGFloat rootViewWidth = CGRectGetWidth(rootViewBounds);

    //Create a rectangle for the toolbar 
    CGRect rectArea = CGRectMake(0, rootViewHeight - toolbarHeight, rootViewWidth, toolbarHeight);

    //Reposition and resize the receiver 
    [toolbar setFrame:rectArea];

    //Create a button 
    UIBarButtonItem *infoButton = [[UIBarButtonItem alloc] initWithTitle:@"back"
                                                                   style:UIBarButtonItemStyleBordered 
                                                                  target:self 
                                                                  action:@selector(info_clicked:)];

    [toolbar setItems:[NSArray arrayWithObjects:infoButton,nil]];

    //Add the toolbar as a subview to the navigation controller.
    [self.navigationController.view addSubview:toolbar];

    [[self tableView] reloadData];
}

(void) info_clicked:(id)sender {

    [self.navigationController popViewControllerAnimated:YES];
    [toolbar removeFromSuperview];

}


推荐答案

更简单要做的是在 UINavigationController 之上构建项目。它已经有一个工具栏,默认情况下它只是隐藏了。您可以通过切换 toolbarHidden 属性来显示它,只要它在导航控制器层次结构中,您的表视图控制器就能够使用它。

The simpler thing to do is to build your project on top of a UINavigationController. It already has a toolbar, it's just hidden by default. You can reveal it by toggling the toolbarHidden property, and your table view controller will be able to use it as long as it's in the navigation controller hierarchy.

在您的app委托中,或者您的app委托传递控件的对象中,使用 UITableViewController 创建导航控制器作为根视图控制器:

In your app delegate, or in the object your app delegate passes control to, create the navigation controller with your UITableViewController as the root view controller:

- ( void )application: (UIApplication *)application
          didFinishLaunchingWithOptions: (NSDictionary *)options
{
    MyTableViewController         *tableViewController;
    UINavigationController        *navController;

    tableViewController = [[ MyTableViewController alloc ]
                                 initWithStyle: UITableViewStylePlain ];
    navController = [[ UINavigationController alloc ]
                           initWithRootViewController: tableViewController ];
    [ tableViewController release ];

    /* ensure that the toolbar is visible */
    navController.toolbarHidden = NO;
    self.navigationController = navController;
    [ navController release ];

    [ self.window addSubview: self.navigationController.view ];
    [ self.window makeKeyAndVisible ];
}

然后在 MyTableViewController中设置工具栏项 object:

Then set the toolbar items in your MyTableViewController object:

- ( void )viewDidLoad
{
    UIBarButtonItem            *buttonItem;

    buttonItem = [[ UIBarButtonItem alloc ] initWithTitle: @"Back"
                                            style: UIBarButtonItemStyleBordered
                                            target: self
                                            action: @selector( goBack: ) ];
    self.toolbarItems = [ NSArray arrayWithObject: buttonItem ];
    [ buttonItem release ];

    /* ... additional setup ... */
}

这篇关于如何以编程方式将UIToolbar添加到UITableViewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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