如何以编程方式将UIToolbar添加到UITableViewController?

jin*_*ini 32 iphone uitableview uitoolbar ios

我选择使用UITableViewController没有笔尖的.我需要一个底部带有两个按钮的UIToolbar.这样做最简单的方法是什么?

PS我知道我可以轻松地使用a UIViewController并添加一个UITableView但是我希望在应用程序中看起来一致.

有人可以帮忙吗?

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

(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];

}
Run Code Online (Sandbox Code Playgroud)

mor*_*ion 78

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

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

- ( 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 ];
}
Run Code Online (Sandbox Code Playgroud)

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

- ( 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 ... */
}
Run Code Online (Sandbox Code Playgroud)

  • 这是一个惊人的建议.我从来不知道UINavigationController有一个默认隐藏的工具栏.我已经在使用它,所以这是一个真正的奖金.还要感谢花时间真正解释一切. (5认同)

Vla*_*yuk 6

您还可以在NavigationController属性检查器中选中"显示工具栏"选项.