我正在制作一个应用程序,屏幕上有三个按钮。必须始终选择其中一个按钮。当应用程序加载时,第一个按钮被设置为选中,并执行其操作。当用户按下另一个按钮时,当前按钮被取消选择并选择新按钮。
我对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
按钮将有一个会改变的背景图像