xcode 当故事板中的按钮被点击时不同的图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12150614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Different image when button from storyboard tapped
提问by Peter V
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];
But the button is in a .storyboard file, not code.
What should I use in this situation?
但是按钮位于 .storyboard 文件中,而不是代码中。
在这种情况下我应该使用什么?
回答by DrummerB
You can set up different properties for different states in IB too, by selecting the state from the drop down menu:
您也可以在 IB 中为不同的状态设置不同的属性,方法是从下拉菜单中选择状态:
If you want to do it in code, you have to set up an outlet and set the images in awakeFromNib
.
如果你想在代码中做到这一点,你必须设置一个插座并将图像设置在awakeFromNib
.
@property (nonatomic, weak) IBOutlet UIButton *button;
To connect the outlet, you Ctrl-drag from the button to your File Owner (probably a view controller) and select the outlet defined above.
要连接插座,您Ctrl从按钮拖动到您的文件所有者(可能是视图控制器)并选择上面定义的插座。
Then you can access the button in your code:
然后您可以访问代码中的按钮:
- (void)awakeFromNib {
[button setBackgroundImage:[UIImage imageNamed:@"buttonDown.png"]
forState:UIControlStateHighlighted];
}
回答by Lorenzo B
Why not using outlets?
为什么不使用网点?
Configuring the Viewfrom Apple doc.
从 Apple 文档配置视图。
Hope that helps.
希望有帮助。