1

我不喜欢 UISegmentedControl 的风格,因此我试图改变它的外观,但我无法将图像附加到它的按钮上。我只是根本没有改变。

现在我正在研究如何使用 4 个 UIButtons 复制该功能。

我在界面生成器中设置了 4 个 UIButton,为它们添加了不同的标签号。

我无法完成的是当一个按钮被点击时,它应该被“选中”而其他按钮应该被取消选中。

我怎样才能将它们全部连接起来?

如果有办法改变 UISegmented 控件的外观,我将需要所有这些努力。

谢谢你们的帮助

这是针对 iPhone 操作系统的

4

1 回答 1

3

使用 4 个按钮,关闭所有其他按钮并不难。以下是一些帮助您入门的代码:

- (void)buttonPressed:(NSInteger)activeButtonIndex {
    for (int i = 0; i < [buttons count]; i++) {
        MYButton *button = [buttons objectAtIndex:i];
        if (i == activeButtonIndex) {
            [button setDepressed:YES];
        } else {
            [button setDepressed:NO];
        }
    }
}
- (void)button1Pressed:(id)sender { [self buttonPressed:1]; }
- (void)button2Pressed:(id)sender { [self buttonPressed:2]; }
- (void)button3Pressed:(id)sender { [self buttonPressed:3]; }
- (void)button4Pressed:(id)sender { [self buttonPressed:4]; }

此处需要注意的重要设置:

  1. 您将需要子类化您的 uibutton 以使其具有depressed状态。这样,如果选择了一个项目,您只需更换按钮的图像或背景,使其看起来很压抑。
  2. buttons是一个用这个子类化的 uibutton 对象填充的数组。你可以在 viewDidLoad 中设置它;
于 2010-04-22T13:12:52.883 回答