gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 07:46:17 26 4
gpt4 key购买 nike

我选择使用没有 Nib 的 UITableViewController。我需要一个底部有两个按钮的 UIToolbar。最简单的方法是什么?

附言我知道我可以轻松地使用 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];

}

最佳答案

更简单的做法是在 UINavigationController 之上构建您的项目。它已经有一个工具栏,它只是默认隐藏。您可以通过切换 toolbarHidden 属性来显示它,只要它在导航 Controller 层次结构中,您的 TableView Controller 就可以使用它。

在您的应用程序委托(delegate)中,或在您的应用程序委托(delegate)将控制权传递给的对象中,使用您的 UITableViewController 作为 Root View Controller 创建导航 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 对象中设置工具栏项:

- ( 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 ... */
}

关于iphone - 如何以编程方式将 UIToolbar 添加到 UITableViewController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5958956/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com