简体   繁体   English

单击更改UIButton的图像将不起作用

[英]Changing image of UIButton on click won't work

I'm trying to change images when clicked on three different UIButtons. 我试图在单击三个不同的UIButton时更改图像。 Here's my code: 这是我的代码:

-(void)One:(UIButton*)sender{
    [facebook setImage:[UIImage imageNamed:@"tab_selected.png"] forState:UIControlStateNormal];
    [twitter setImage:[UIImage imageNamed:@"tab_unselected.png"] forState:UIControlStateNormal];
    [twittertag setImage:[UIImage imageNamed:@"tab_unselected.png"] forState:UIControlStateNormal];
}

#pragma mark change image of second button
-(void)Two:(UIButton*)sender{
    [facebook setImage:[UIImage imageNamed:@"tab_unselected.png"] forState:UIControlStateNormal];
    [twitter setImage:[UIImage imageNamed:@"tab_selected.png"] forState:UIControlStateNormal];
    [twittertag setImage:[UIImage imageNamed:@"tab_unselected.png"] forState:UIControlStateNormal];
}

#pragma mark change image of third button
-(void)Three:(UIButton*)sender{
    [facebook setImage:[UIImage imageNamed:@"tab_unselected.png"] forState:UIControlStateNormal];
    [twitter setImage:[UIImage imageNamed:@"tab_unselected.png"] forState:UIControlStateNormal];
    [twittertag setImage:[UIImage imageNamed:@"tab_selected.png"] forState:UIControlStateNormal];
}

Or should I put it in the viewDidLoad method like this? 还是应该像这样将其放在viewDidLoad方法中?

if (facebook.selected) {
        [facebook setImage:[UIImage imageNamed:@"tab_selected.png"] forState:UIControlStateSelected];
        [twitter setImage:[UIImage imageNamed:@"tab_unselected.png"] forState:UIControlStateNormal];
        [twittertag setImage:[UIImage imageNamed:@"tab_unselected.png"] forState:UIControlStateNormal];
    } else if (twitter.selected) {
        [facebook setImage:[UIImage imageNamed:@"tab_unselected.png"] forState:UIControlStateNormal];
        [twitter setImage:[UIImage imageNamed:@"tab_selected.png"] forState:UIControlStateSelected];
        [twittertag setImage:[UIImage imageNamed:@"tab_unselected.png"] forState:UIControlStateNormal];
    } else if (twittertag.selected) {
        [facebook setImage:[UIImage imageNamed:@"tab_unselected.png"] forState:UIControlStateNormal];
        [twitter setImage:[UIImage imageNamed:@"tab_unselected.png"] forState:UIControlStateNormal];
        [twittertag setImage:[UIImage imageNamed:@"tab_selected.png"] forState:UIControlStateSelected];
    }

I've connected the UIButtons facebook, twitter & twittertag to the buttons in IB, but nothing happens when I click on the buttons in the app. 我已经将UIButtons facebook,twitter和twittertag连接到IB中的按钮,但是当我单击应用程序中的按钮时没有任何反应。

Why? 为什么? Can someone explain this? 有人可以解释吗?

You need to set the image for state UIControlStateSelected rather than UIControlStateNormal . 您需要将图像设置为状态UIControlStateSelected而不是UIControlStateNormal

Also, you should do this in the viewDidLoad or some other method, not right when the button is clicked. 另外,您应该在viewDidLoad或其他方法中执行此操作,而不是在单击按钮时正确执行此操作。

EDIT: 编辑:

Think about CSS. 考虑一下CSS。 Sometimes you might want to set what the link color is when someone hovers over it or when someone has already clicked it. 有时,您可能希望设置当有人将鼠标悬停在其上或已单击它时的链接颜色。

You don't do this "at the time of the hover/click." 您不必在“悬停/单击时”执行此操作。 You do this up-front, in the beginning, by setting what the style will be mouseOver or visited . 你这样做的前期,在开始时,通过设定什么风格将mouseOvervisited

In other words, you are telling your program, WHEN someone selects my button, if it ever even happens, make the image be this. 换句话说,您是在告诉您的程序,当有人选择我的按钮时,即使它发生了,也要使图像变成这样。

You don't have to wait until someone clicks to know what image will be used when it is clicked. 您不必等到有人单击就知道单击后将使用什么图像。 The on-selected image in your example is already known before the click. 单击之前已经知道示例中的选定图像。 It would be different if, for instance, the selected image depended on the part of the button you clicked. 例如,如果所选图像取决于您单击的按钮部分,则将有所不同。

Another way to put it: say you are a boss and need to give your team instructions on what to do in case a fire breaks out. 另一种说法是:说您是老板,需要向团队提供有关如何避免发生大火的指示。

If you don't care about which team member to whom you are providing instructions or where the fire is, etc, you might say: "When a fire breaks out, use Exit B to leave the building." 如果你不关心哪个队员你是谁提供的指令或者火,等等,你可能会说:“当发生火灾时,使用B出口离开大楼”

This is like what [facebook setImage: forState: ] is doing, where image is like use Exit B to leave the building and state is like Fire in the previous example. 这就像[facebook setImage: forState: ]所做的那样,其中image类似于use Exit B to leave the buildingstate类似于上一个示例中的Fire

Now say you want to provide instructions based on where the fire is. 现在说您想根据火灾发生的地方提供指示。

You would need to add another parameter to specify the location, ie: 您将需要添加另一个参数来指定位置,即:

[teamResponse setAction:@"EXIT B" forState:"FIRE" forLocation:2];

[teamResponse setAction:nil forState:"FIRE" forLocation:1];

In the first line, you are saying "when there is a fire and the location is 2, do action EXIT B." 在第一行中,您说的是“当着火并且位置为2时,请执行出口B”。

In the second, you are saying "when there is a fire and the location is 1, do nothing." 在第二个中,您说的是“着火时位置为1,则不执行任何操作”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM