iphone - 无法将按钮添加到 TableView 工具栏

标签 iphone objective-c ios xcode

所以我有一个带 TableView 的 Root View 。我这样显示工具栏:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.navigationController.toolbarHidden = NO;
}

然后我实现了 setToolbarItems 方法:

- (void)setToolbarItems:(NSArray *)toolbarItems animated:(BOOL)animated
{
    UIBarButtonItem *buttonItem;
    buttonItem = [[UIBarButtonItem alloc] initWithTitle:@"Hello" style:UIBarButtonItemStyleDone target:self action:@selector(goBack:)];
    self.navigationController.toolbarItems = [ NSArray arrayWithObject: buttonItem ];
}

结果是一个空的工具栏。为什么?

enter image description here

最佳答案

来自文档:

toolbarItems The toolbar items associated with the view controller.

@property(nonatomic, retain) NSArray *toolbarItems Discussion This property contains an array of UIBarButtonItem objects and works in >conjunction with a UINavigationController object. If this view controller is >embedded inside a navigation controller interface, and the navigation >controller displays a toolbar, this property identifies the items to display in >that toolbar.

You can set the value of this property explicitly or use the >setToolbarItems:animated: method to animate changes to the visible set of >toolbar items.

换句话说,尝试通过实际的 View Controller 访问它,而不是像这样的导航 Controller :

self.toolbarItems = [ NSArray arrayWithObject: buttonItem ];

关于iphone - 无法将按钮添加到 TableView 工具栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10059442/

相关文章:

iphone - 多个注释数组,每个数组的引脚颜色不同?

iphone - 在 iOS 中获取 CGImageRef 的平均 RGB 颜色

objective-c - 在xcode中启用标签?或者减轻没有它们的痛苦?

ios - 将第一个 UICollectionViewCell 滚动到 View 中心

iphone - 如何在按钮标题中创建带有标签和值对的 UIActionSheet

iphone - 录制的视频方向问题

iphone - 如何将 ManagedObjectContext 传递给 TabBarViewController

ios - 滚动 UIImageView 图像

ios - 禁用 iPad 的 UI 交互,直到数据在后端的主线程上下载

ios - 从 UIImageView 裁剪图像