如何使用无线电界面创建 UIBarButtonItems?

发布于 2024-08-10 19:05:01 字数 526 浏览 6 评论 0原文

我有一个 UIToolbar,需要三个单选按钮,这意味着在这三个按钮中,一次只能按下一个按钮。该文档提到了在 width 属性的类参考定义中设置单选 UIBarButtonItems 的可能性:

如果该属性值为正, 组合图像的宽度和 标题是固定的。如果值为 0.0 或负数,该项目设置宽度 组合图像和标题的 合身。 如果 样式使用单选模式。默认 值为 0.0。

但是,我在 UIKit Framework Reference 中找到了“radio”,但找不到任何关于 radio 风格的 UIBarButtonItems 的提及。我知道我可以选择使用 TabBar 作为单选界面,但 TabBar 不太符合我的 UI 的目的(普通按钮 + 单选按钮)。我看到日历应用程序以单选样式(列表、日、月)使用 UIBarButtonItems,因此看起来这应该位于 API 中的某个位置并得到 HIG 的批准。这是隐藏在某个地方还是我必须使用自定义视图创建 UIBarButtonItems ?

I have a UIToolbar that needs three buttons in a radio style, meaning that of the three, only one button can be pushed at a time. The documentation makes reference to the possibility of setting up radio UIBarButtonItems in the class reference definition of the width property:

If this property value is positive,
the width of the combined image and
title are fixed. If the value is 0.0
or negative, the item sets the width
of the combined image and title to
fit. This property is ignored if the
style uses radio mode.
The default
value is 0.0.

However, I did a find for "radio" in the UIKit Framework Reference and I can't find any mention of UIBarButtonItems in radio style. I know that I could alternatively use a TabBar for a radio interface, but a TabBar doesn't quite match the purpose of my UI (normal buttons + radio buttons). I see that the Calendar App uses UIBarButtonItems in a radio style (List, Day, Month), so it seems like this should be somewhere in the API and approved by the HIG. Is this hiding somewhere or would I have to create UIBarButtonItems with custom views?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

那小子欠揍 2024-08-17 19:05:01

UISegmentedControl 就是您想要的。它有点隐藏在 Interface Builder 中,因为它是工具栏之外的不同样式。

正常样式:

正常分段控件

工具栏中的相同内容:

bar分段控制

对于它的行为,您有两种选择:点击时短暂突出显示,或无线电风格的行为,这正是您想要的。您可以使用属性检查器中的“瞬时”复选框进行设置:

分段控制属性

A UISegmentedControl is what you want. It's kind of hiding in Interface Builder, as it's a different style outside of a toolbar.

The normal style:

normal segmented control

The same thing in a toolbar:

bar segmented control

You have two options for its behavior: a momentary highlight when tapped, or a radio-style behavior, which is what you want. You can set this with the "Momentary" checkbox in the Attributes inspector:

segmented control attributes

感情洁癖 2024-08-17 19:05:01

您是否考虑过尝试 UISegmentedControl?您可以将其设置为一次仅“按下”其中一个片段。

Have you considered trying a UISegmentedControl? You can set it up so that only one of the segments is "pressed" at a time.

伏妖词 2024-08-17 19:05:01

如果您需要在 objc 中实时执行此操作,

  1. 请使用您选择的单选按钮创建一个搅拌数组
  2. ,并将您的 UISegmentedControl 放入 UIBarButtonItem 中。
  3. 将 UIBarButtonItem 添加到您的 navigationItem

对我有用。代码在下面

NSArray *itemArray = [NSArray arrayWithObjects: @"One", @"Two", @"Three", nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
segmentedControl.frame = CGRectMake(35, 200, 250, 50);
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
segmentedControl.selectedSegmentIndex = 1;

// Uncomment this part if you need to do something when ratio state is selected. Also paste the function at the end of this post somewhere in your class.
// [segmentedControl addTarget:self   action:@selector(pickOne:) forControlEvents:UIControlEventValueChanged];

UIBarButtonItem *bbi= [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[segmentedControl release];


/* 
//and paste this function somethere in your class. It prints the label in debug terminal
- (void) pickOne:(id)sender{
UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;
NSLog(@"%@", [segmentedControl titleForSegmentAtIndex: [segmentedControl selectedSegmentIndex]]);
} 
*/

我使用了这篇(不是我的)帖子中的一些代码 http://howtomakeiphoneapps.com/here-is-how-you-use-the-segmented-control-uisegmentedcontrol/129/

If you need to do this in objc in real time

  1. Create an array of stirings with your choices for radio button
  2. create and put your UISegmentedControl into a UIBarButtonItem.
  3. add that UIBarButtonItem to your navigationItem

worked for me. Code is below

NSArray *itemArray = [NSArray arrayWithObjects: @"One", @"Two", @"Three", nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
segmentedControl.frame = CGRectMake(35, 200, 250, 50);
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
segmentedControl.selectedSegmentIndex = 1;

// Uncomment this part if you need to do something when ratio state is selected. Also paste the function at the end of this post somewhere in your class.
// [segmentedControl addTarget:self   action:@selector(pickOne:) forControlEvents:UIControlEventValueChanged];

UIBarButtonItem *bbi= [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[segmentedControl release];


/* 
//and paste this function somethere in your class. It prints the label in debug terminal
- (void) pickOne:(id)sender{
UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;
NSLog(@"%@", [segmentedControl titleForSegmentAtIndex: [segmentedControl selectedSegmentIndex]]);
} 
*/

I used some code from this (not mine) post http://howtomakeiphoneapps.com/here-is-how-you-use-the-segmented-control-uisegmentedcontrol/129/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文