1

我正在制作一个应用程序,屏幕上有三个按钮。必须始终选择其中一个按钮。当应用程序加载时,第一个按钮被设置为选中,并执行其操作。当用户按下另一个按钮时,当前按钮被取消选择并选择新按钮。

我对Objective-C有点陌生。这是我想出的,但我所期望的都没有。我已经在标题中设置了按钮插座。



    - (IBAction)buttonSelector:(id)sender
    {
        firstButton.selected = YES;
        secondButton.selected = NO;
        thirdButton.selected = NO;

        if (firstButton.selected = YES)
        {
            [firstbutton setBackgroundImage:[UIImage imageNamed:"selected.png"]];
            secondButton.selected = NO;
            [secondButton setBackgroundImage:[UIImage imageNamed:"un_selected.png"]];
            thirdButton.selected = YES;
            [thirdButton setBackgroundImage:[UIImage imageNamed:"un_selected.png"]];
        } else if (secondButton.selected = YES)
        {
            [secondButton setBackgroundImage:[UIImage imageNamed:"selected.png"]];
            firstButton.selected = NO;
            [firstButton setBackgroundImage:[UIImage imageNamed:"un_selected.png"]];
            thirdButton.selected = NO;
            [firstButton setBackgroundImage:[UIImage imageNamed:"un_selected.png"]];
        } else if (thirdButton.selected = YES)
        {
            [thirdButton setBackgroundImage:[UIImage imageNamed:"selected.png"]];
            firstButton.selected = NO;
            [firstButton setBackgroundImage:[UIImage imageNamed:"un_selected.png"]];
            secondButton.selected = NO;
            [secondButton setBackgroundImage:[UIImage imageNamed:"un_selected.png"]];
        } else {
            [sender setBackgroundImage:[UIImage imageNamed:"un_selected.png"]];
        }

    }

编辑1:

我尝试了 UISegmentedControl,但它似乎无法针对我正在寻找的内容进行足够的定制。理想情况下,按钮将更多地遵循以下原则:http: //i.stack.imgur.com/LPwoR.jpg

按钮将有一个会改变的背景图像

4

2 回答 2

5

你应该使用UISegmentControl这个。这正是您正在寻找的功能。这是关于此的苹果文档

这是 Ray wenderlich关于如何自定义 UI 元素的教程,其中包括UISegmentControl.

例如:-

NSArray *itemArray = [NSArray arrayWithObjects: @"One", @"Two", @"Three", nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
segmentedControl.frame = CGRectMake(35, 200, 250, 50);
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
于 2012-12-04T18:34:16.877 回答
0

只需使用 for 循环执行此任务,首先只需将图像设置为所有按钮,然后更改所选图像的图像。

于 2012-12-04T19:07:39.693 回答