gpt4 book ai didi

iphone - 将项目添加到导航栏(不使用 UINavigationController)

转载 作者:行者123 更新时间:2023-12-03 18:29:17 25 4
gpt4 key购买 nike

我有一个 UIViewController,其中有一个 UITableView,还添加了一个 UINavigationBar。如何以编程方式在该栏中添加和“编辑”按钮和“+”按钮?(我尝试过使用IB,但标题总是被替换,并且没有添加其他项目)我没有使用 UINavigationController。我的 UIViewController 是独立的吗?

这是我尝试过但没有成功的方法:

UIBarButtonItem *barButton = 
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStyleBordered
target:nil
action:nil];
UINavigationItem *editItem = [[UINavigationItem alloc] initWithTitle:@"Title"];
[editItem setLeftBarButtonItem:barButton animated:YES];
[navigationBar setItems:[NSArray arrayWithObject:editItem] animated:YES];

最佳答案

您的UIViewController有一个navigationItem属性。您可以使用 self.navigationItem.leftBarButtonItem = ...self.navigationItem.rightBarButtonItem = ...

设置左右栏按钮项

编辑:

好的,我假设您有 UINavigationBar 的引用?然后我猜你会向其中添加一个 UINavigationItem :

UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:@"A Title"];
theNavigationBar.items = [NSArray arrayWithObject:item];
[item release]; // or keep this as an instance variable

然后设置该项目的左右按钮:

theNavigationBar.topItem.leftBarButtonItem = ...;
theNavigationBar.topItem.rightBarButtonItem = ...;

我还没有尝试过,但我认为它应该有效。

关于iphone - 将项目添加到导航栏(不使用 UINavigationController),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3606161/

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