简体   繁体   English

点击时,UIButton图像不会改变

[英]UIButton image doesn't change when tapped

I need to have an image button change to a different image when I tap it. 当我点击图像按钮时,需要将其更改为其他图像。 Simple enough, and there are samples out there, but they don't work for me. 很简单,这里有示例,但是它们对我不起作用。 This is what I tried following the examples: 这是我尝试遵循的示例:

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(20, 270, 200, 40.0);
[button setImage:[UIImage imageNamed:@"butterfly3.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"clicked.png"] forState:UIControlStateSelected | UIControlStateHighlighted];
[button addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Submit" forState:UIControlStateNormal];
[scrollView button];

Now, I notice that my button is set to perform its action on UIControlEventTouchDown so I added it to the "clicked" control states like this: 现在,我注意到我的按钮设置为在UIControlEventTouchDown上执行其操作,因此我将其添加到“已单击”控件状态,如下所示:

[button setImage:[UIImage imageNamed:@"clicked.png"] forState:UIControlStateSelected | UIControlStateHighlighted | UIControlEventTouchDown];

but the image still stays the same. 但是图像仍然保持不变。 What am I missing? 我想念什么?

The issue was that my buttonPressed method did a long(ish) query on the main thread and for some reason the highlighting of the button doesn't work in such a case. 问题是我的buttonPressed方法在主线程上做了很长的查询,由于某种原因,在这种情况下按钮的高亮显示不起作用。

When I moved the query to a separate thread the highlight went back on. 当我将查询移到一个单独的线程时,突出显示继续。

For the sake of completion I'll bring the code here. 为了完整起见,我将代码带到这里。 Initially I was fetching objects through Parse.com API like this: 最初,我是通过Parse.com API来获取对象的,如下所示:

[myQuery findObjects]

This above does the query on the main thread and blocks everything else. 上面的代码在主线程上执行查询,并阻塞其他所有内容。

Then I changed it to fetching in a separate thread like this: 然后,我将其更改为在这样的单独线程中获取:

[myQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if(!error) {
            // whatever is needed to be done
        }
    } else {
        NSLog(@"Error while fetching objects %@", error.description);
    }
}];

Just change your code by this, and everything will work! 只需更改您的代码,一切都会正常!

[button setImage:[UIImage imageNamed:@"clicked.png"] forState:UIControlStateHighlighted];
[button setImage:[UIImage imageNamed:@"clicked.png"] forState:UIControlStateSelected];

Inside buttonPressed method set yourButton.selected = YES; 内部buttonPressed方法设置yourButton.selected = YES; Also set image as Miroslav mentioned. 也像米罗斯拉夫提到的那样设置形象。

It appears that you are putting an event mask UIControlEventTouchDown that is being interpreted as a state mask in this line: 似乎您正在将事件掩码UIControlEventTouchDown放置在此行中,该事件掩码被解释为状态掩码:

[button setImage:[UIImage imageNamed:@"clicked.png"] forState:UIControlStateSelected | UIControlStateHighlighted | UIControlEventTouchDown];

In fact I think you may not need this line at all, as you've already set the appropriate images. 实际上,我认为您可能根本不需要此行,因为您已经设置了适当的图像。 Try removing UIControlEventTouchDown from this line or comment out the whole line. 尝试从此行中删除UIControlEventTouchDown或注释掉整个行。

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

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