使用 UIButtons 复制 UISegmentedControl - iPhone

发布于 2024-08-30 01:17:17 字数 319 浏览 6 评论 0原文

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

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

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

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

我怎样才能连接所有这些?

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

感谢大家的帮助,

这是针对 iPhone 操作系统的

I didnt like the style of UISegmentedControl, hence i tried to change the way it looked, but I couldnt attach images to its buttons. I just didnt change at all.

Now i'm looking at how to replicate that function with 4 UIButtons.

I've setup 4 UIButtons in interface builder, added different tag numbers to them.

What i cannot accomplish is when a button is tapped, it should be "selected" and the other buttons should be Unselected.

How can i connect all of them?

And if there was a way to change the UISegmented control looks, i would need all this effort.

thanks for the help guys

this is for iPhone OS

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

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

发布评论

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

评论(1

音盲 2024-09-06 01:17:17

使用 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 中设置它;

Using 4 buttons, it is not too difficult to turn off all the others. Here is some code to get you started:

- (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]; }

Important setup to note here:

  1. You will need to subclass your uibutton to make it have a depressed state. This way, if an item is selected, you simply swap out the image or background of the button to make it looked depressed.
  2. buttons is an array that is filled with this subclassed uibutton object. You would set it up in viewDidLoad;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文