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/

相关文章:

ipad - 确定网站是作为网络应用程序打开还是通过 iPad 上的常规 safari 打开?

cocoa - 如何使用 Cocoa 实现布局编辑器

ios - 无法更改为 IB 中的不同尺寸类别定义的 Constraint IBOutlet

iphone - 如何在 IOS safari 上禁用缓存?

ios - 使用 AFNetworking 2.0 分组或批量请求

ios - 使用 arch i386 为 iPhone 模拟器编译 C 库?

ios - Phonegap Twitter 插件的 getTWRequest 抛出 NSInvalidArgumentException

javascript - 模拟滑动 slider 的 iPad WebApp

ios - 任何设备的 Storyboard 安全区域错误/固定宽度

ios - 有什么方法可以知道 UITextField 是否在 View 的层次结构中?