ios - 在 iOS 5 中使用 Storyboard时无法将多个按钮分配给 UINavigationItem

标签 ios ipad interface-builder storyboard uinavigationitem

我是一名 iOS 开发人员,在通过代码开发 UI 方面拥有丰富的经验。

我现在正在测试 Storyboard 功能,因为我测试切换到“设计”UI 而不是实现它。过去,我在使用 nib/xib 时遇到了很多限制,因此我从未成功地进行过转换。所以这是 Storyboard的新尝试:)

现在我的问题 - 我正在设计一个 iPad Storyboard,它有一个导航 Controller 和一个 Tableview Controller 。我想添加多个 UIBarButtonItem,但我可以使用 Interface Builder 为每一侧添加一个。

代码如下:

UIBarButtonItem *b = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:nil action:nil];

UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
spacer.width = 20;

self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:self.editButtonItem, spacer, b, nil];

但为什么我不能使用 IB 添加多个按钮?只有 leftBarButtonItem、rightBarButtonItems、backBarButtonItems 的导出...

这让我发疯了¡¡¡

谢谢!

最佳答案

我找到了一个简单的解决方案。

1) 添加以下类别:

@interface UINavigationItem(MultipleButtonsAddition)
@property (nonatomic, strong) IBOutletCollection(UIBarButtonItem) NSArray* rightBarButtonItemsCollection;
@property (nonatomic, strong) IBOutletCollection(UIBarButtonItem) NSArray* leftBarButtonItemsCollection;
@end

@implementation UINavigationItem(MultipleButtonsAddition)

- (void) setRightBarButtonItemsCollection:(NSArray *)rightBarButtonItemsCollection {
    self.rightBarButtonItems = [rightBarButtonItemsCollection sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"tag" ascending:YES]]];
}

- (void) setLeftBarButtonItemsCollection:(NSArray *)leftBarButtonItemsCollection {
    self.leftBarButtonItems = [leftBarButtonItemsCollection sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"tag" ascending:YES]]];
}

- (NSArray*) rightBarButtonItemsCollection {
    return self.rightBarButtonItems;
}

- (NSArray*) leftBarButtonItemsCollection {
    return self.leftBarButtonItems;
}

@end

2) 将您的项目添加到 View Controller (项目将按标签升序排序)

enter image description here

3) 将您的项目与 UINavigationItem 的 leftBarButtonItemsCollectionrightBarButtonItemsCollection 导出集合连接

enter image description here

关于ios - 在 iOS 5 中使用 Storyboard时无法将多个按钮分配给 UINavigationItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9897978/

相关文章:

ios - "Pause"和 "Resume"AVAssetDownloadTask 有可能吗?

ios - 如何使用SDWebImage获取缩略图?

ios - 如何在 Swift 3 中创建调度队列

IOS Swift - 如何在具有不同数据的 Collection View 中使用 2 个 TableView

iphone - 当已分配给特定 ivar 时将对象分配给 ivar

iphone - Xcode 4.3 和在 iOS 3.x 设备上调试

iphone - 适用于 iPhone 和 iPad 的最佳图形和图表库是什么?

iOS 应用程序 : UIButton tag not available in controller code

cocoa - 重新计算KeyViewLoop 不起作用

iphone - 如何将 UIScrollView 添加到界面构建器?