在 iOS 5 中使用 Storyboard 时,无法将多个按钮分配给 UINavigationItem [英] Can't assign multiple Buttons to UINavigationItem when using Storyboard with iOS 5

查看:19
本文介绍了在 iOS 5 中使用 Storyboard 时,无法将多个按钮分配给 UINavigationItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm a iOS developer with a lot experience in developing the UI by code.

我现在正在测试 Storyboard 功能,因为我正在测试切换到设计"UI 而不是实现它.过去,我在使用 nib/xib 时遇到了很多限制,因此我从未成功使用 switch.所以这是故事板的新尝试:)

I'm now testing the Storyboard functionality, because I testing to switch to "design" the UI rather then implementing it. In the past I stuck with to much limits using nib/xib's and therefore I never succeed with the switch. So here comes a new try with storyboading :)

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

Now my question - I'm designing an iPad Storyboard which has a Navigation Controller and a Tableview Controller. I want to add multiple UIBarButtonItems, but I can just add one for each side with the 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 的插座...

But why can't I add multiple buttons using the IB? There are only outlets for leftBarButtonItem, rightBarButtonItems, backBarButtonItems...

这让我发疯了¡¡¡

谢谢!

推荐答案

EDIT

在我回答这个问题时,Xcode 没有提供链接故事板中添加按钮的可能性.所呈现的技巧允许仍然在故事板中设计转场.

At the time I answered this question, Xcode was not offering the possibility of linking added buttons in the storyboard. The trick presented permitted to still have the segues designed in the storyboard.

随着 Xcode 的更新版本,@ecotax 引入的解决方案和后来@ShimanskiArtem 的更详细的答案肯定是要使用的.

With more recent versions of Xcode, for sure the solution introduced by @ecotax and later the more detailed answer of @ShimanskiArtem are the ones to be used.

我遇到了和你一样的问题,我发现了以下技巧

I had the same problem as you and I found the following trick

假设您有一个导航控制器,您希望在其中拥有多个按钮.从 iOS 5 开始,您可以分配一个数组.问题是您失去了使用故事板的所有好处,因为它将以编程方式完成.

Suppose you have a navigationController in which you would like to have multiple buttons. Since iOS 5 you can assign an array. The problem is that you lose all the benefits of using the storyboard as it will be done programmatically.

我使用了以下技巧.通常,当您需要导航栏上的多个按钮时,您不需要工具栏.

I used the following trick. Usually when you want multiple button on the navigation bar you don't want a toolbar.

在您希望按钮出现的当前视图(而不是在导航控制器中)中,通过更改显示工具栏

In the current view (not in the navigation controller) where you want the buttons to appear, show the toolbar by changing

bottomBar = 推断到 bottomBar = 工具栏.

bottomBar = inferred to bottomBar = toolbar.

底部会出现一个工具栏.将 UIBarButtons 添加到此栏.使用 segue 等将它们链接到其他视图控制器......

A toolbar will appear at the bottom. Add UIBarButtons to this bar. Link them to other view controllers using segues, etc ...

在您的 .h 文件中为每个按钮创建一个插座

in your .h file create an outlet for each button

@property (strong, nonatomic) IBOutlet UIBarButtonItem *Button1;
@property (strong, nonatomic) IBOutlet UIBarButtonItem *Button2;
@property (strong, nonatomic) IBOutlet UIBarButtonItem *Button3;

然后在您的 viewDidLoad() 中将按钮链接到导航栏并隐藏工具栏.以与您要查看它们的顺序相反的顺序添加它们

Then in your viewDidLoad() link the buttons to the navigation bar and hide the toolbar. Add them in the reverse order of the order you want to see them

self.navigationItem.rightBarButtonItems =
    [NSArray arrayWithObjects:self.Button3, self.Button2, self.Button1, nil];

self.navigationController.toolbarHidden = YES;

瞧,您的导航栏中有多个按钮

And voilà you have multiple buttons in your navigation bar

和模拟器中的结果

这篇关于在 iOS 5 中使用 Storyboard 时,无法将多个按钮分配给 UINavigationItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆