1

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

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

但是,我在 UIKit 框架参考中找到了“收音机”,但在收音机样式中找不到任何提及 UIBarButtonItems 的内容。我知道我可以选择将 TabBar 用于单选界面,但 TabBar 与我的 UI 的目的不太匹配(普通按钮 + 单选按钮)。我看到日历应用程序以单选样式(列表、日、月)使用 UIBarButtonItems,所以看起来这应该在 API 中的某个地方并得到 HIG 的批准。这是隐藏在某处还是我必须使用自定义视图创建 UIBarButtonItems?

4

3 回答 3

7

UISegmentedControl 是您想要的。它有点隐藏在 Interface Builder 中,因为它是工具栏之外的另一种风格。

正常风格:

正常分段控制

工具栏中的相同内容:

条形分段控制

它的行为有两种选择:点击时的瞬间高亮,或无线电风格的行为,这是你想要的。您可以使用属性检查器中的“瞬间”复选框进行设置:

分段控制属性

于 2009-11-14T02:06:57.043 回答
1

您是否考虑过尝试 UISegmentedControl?您可以对其进行设置,以便一次仅“按下”其中一个段。

于 2009-11-14T01:56:44.117 回答
0

如果您需要在 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/

于 2012-02-06T14:59:35.093 回答