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

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

问题描述

我是iOS开发人员,在使用代码开发UI方面有很多经验。

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

我现在正在测试Storyboard功能,因为我测试切换到用户界面设计而不是实现它。在过去,我坚持使用nib / xib的极限,因此我从未使用过开关。所以这里有一个关于故事情节的新尝试:)

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故事板,它有一个导航控制器和一个Tableview控制器。我想添加多个UIBarButtonItems,但我可以使用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...

这让我发疯了

谢谢!

推荐答案

编辑

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

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

假设您有一个navigationController,您希望在其中拥有多个按钮。从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 = toolbar。

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

< img src =https://i.stack.imgur.com/tj259.pngalt =在此处输入图像说明>

以及模拟器中的结果

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

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