点击故事板按钮时的不同图像 [英] Different image when button from storyboard tapped
问题描述
我有一个按钮,我想在高亮状态下使用不同的背景图像,通常我会使用类似
I have a button that I want to use a different background image when in highlighted state, normally I would use something like
[buttonObject setBackgroundImage:[UIImage imageNamed:@"buttonDown.png"]
forState:UIControlStateHighlighted];
但按钮位于.storyboard文件中,而不是代码。
什么我应该在这种情况下使用吗?
But the button is in a .storyboard file, not code.
What should I use in this situation?
推荐答案
您也可以通过从下拉菜单中选择状态来为IB中的不同状态设置不同的属性:
You can set up different properties for different states in IB too, by selecting the state from the drop down menu:
如果你想在代码中这样做,你必须设置一个插座并在 awakeFromNib
中设置图像。
If you want to do it in code, you have to set up an outlet and set the images in awakeFromNib
.
@property (nonatomic, weak) IBOutlet UIButton *button;
要连接插座,您可以 Ctrl -drag从按钮到您的文件所有者(可能是视图控制器)并选择上面定义的插座。
To connect the outlet, you Ctrl-drag from the button to your File Owner (probably a view controller) and select the outlet defined above.
然后您可以访问代码中的按钮:
Then you can access the button in your code:
- (void)awakeFromNib {
[button setBackgroundImage:[UIImage imageNamed:@"buttonDown.png"]
forState:UIControlStateHighlighted];
}
这篇关于点击故事板按钮时的不同图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!