简体   繁体   English

IOS:点击时如何更改按钮的背景图像?

[英]IOS: How to change the background image of a button when clicked?

I have a button that when it is clicked, it changes the text of a label. 我有一个按钮,单击它时,它会更改标签的文本。 Here is a snippet of my code from my controller module (happydaysViewController.m): 这是我的控制器模块(happydaysViewController.m)的代码片段:

#import "happydaysViewController.h"

@implementation happydaysViewController

-(IBAction)button {

    label.text = @"pop tart";
    button.image = @"Costa Rican Frog.jpg";
}

So, the label text changes from blank to pop tart, however the background image of the button does not change from blank to Costa Rican Frog.jpg. 因此,标签文本从空白变为弹出蛋挞,但按钮的背景图像不会从空白变为哥斯达黎加Frog.jpg。 In fact, I get the following error: 实际上,我收到以下错误:

'button' undeclared '按钮'未声明

Am I approaching this the wrong way? 我是以错误的方式接近这个吗?

There's quite a lot wrong with this code. 这段代码有很多错误。 Rather than list everything I'll just show you what it should be! 而不是列出所有我只会告诉你它应该是什么!

happydaysViewController.h file: happydaysViewController.h文件:

@interface happydaysViewController : UIViewController

@property (nonatomic, strong) IBOutlet UILabel *label;    //in IB drag this to your label
@property (nonatomic, strong) IBOutlet UIButton *button;    //in IB drag this to your button

-(IBAction)buttonClicked:(UIButton *)sender;    //in IB drag you button to this action

@end

happydaysViewController.m file: happydaysViewController.m文件:

#import "happydaysViewController.h"

@implementation happydaysViewController

@synthesize label;
@synthesize button;

-(IBAction)buttonClicked:(UIButton *)sender
{
    label.text = @"pop tart";
    UIImage *image = [UIImage imageNamed:@"Costa Rican Frog.jpg"];
    [sender setImage:image forState:UIControlStateNormal];
}

@end

So things to fix (in no particular order): 所以要修复的事情(没有特别的顺序):

  1. You can't set an image value with a string, you need to create a UIImage object and load the image file into it. 您不能使用字符串设置图像值,您需要创建一个UIImage对象并将图像文件加载到其中。 Fortunately [UIImage imageNamed:...] makes that very easy. 幸运的是[UIImage imageNamed:...]使这很容易。

  2. Buttons can have multiple images for different states (highlighted, selected, disabled, etc) so you have to specify the sate when you set the image. 按钮可以有不同状态的多个图像(突出显示,选中,禁用等),因此您必须在设置图像时指定状态。

  3. The error you are getting indicates that you haven't actually got a property called button, which may be because you never added it to your class, or it may be because you've named your IBAction method "button" and this is conflicting with the getter method for the property called button, which would also be called "button". 你得到的错误表明你实际上没有一个名为button的属性,这可能是因为你从未将它添加到你的类中,或者可能是因为你已经将你的IBAction方法命名为“按钮”,这与之相矛盾名为button的属性的getter方法,也称为“button”。 In my code I'm passing the button as a parameter to the click method (you'll need to re-bind the button in IB if you change its name), which a) provides another way to get the button and b) changes the name of the method so it won't conflict if you do want to have a property called button on your view controller. 在我的代码中,我将按钮作为参数传递给click方法(如果更改其名称,则需要重新绑定IB中的按钮),a)提供获取按钮的另一种方式,b)更改方法的名称,因此如果您想在视图控制器上拥有一个名为button的属性,它将不会发生冲突。

"Highlighted" is the state of control that occurs when touch enters and exits on control. “突出显示”是触摸进入和退出控制时发生的控制状态。

In this example, we are going to create a UIButton programmatically that will show the button changing on touch up. 在此示例中,我们将以编程方式创建一个UIButton,以显示按钮在触摸时更改。

Create a button 创建一个按钮

- (void)drawButton
{
myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame = CGRectMake(80, 50, 70, 70); //set frame for button

UIImage *buttonImage = [UIImage imageNamed:@"icon6.png"];
[myButton setBackgroundImage:buttonImage forState:UIControlStateNormal];

[myButton setTitle:@"Ok" forState:UIControlStateNormal];
[myButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:myButton];

}

- (void)viewDidLoad {
[super viewDidLoad];

[self drawButton];

}

Write a Button Action, that will change the image on touch up 编写一个按钮操作,将在触摸时更改图像

- (IBAction)buttonClicked:(id)sender 
{

UIImage *buttonImage = [UIImage imageNamed:@"home.png"];
[myButton setBackgroundImage:buttonImage forState:UIControlStateHighlighted];

}

On running the application you will find that it changes the image on touch up. 在运行应用程序时,您会发现它会在触摸时更改图像。

你必须这样写:

[[self button] setImage:[NSImage imageNamed:@"Costa Rican Frog.jpg"]];

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

相关问题 我每次单击按钮时如何更改按钮的背景图像 - how to change the background image of button everytime when i clicked on that button 如何在iphone中点击按钮背景图像? - how can i change button background image when it is clicked in iphone? 单击该按钮时如何在tableview中更改按钮图像 - how to change button image in tableview when that button clicked 按下或单击按钮时如何显示图像背景 - How to show a image background when button is scelected or clicked 单击按钮时如何更改uiwebview的URL? - how to change url of uiwebview when button is clicked? 如何使用三个具有背景图像和tagID的按钮创建自定义表格单元格。单击按钮时我想返回标签ID? - How to create custom tableview cell with three buttons having background image and tagID.I want to return tag ID when button is clicked? 更改以编程方式创建的单击的UIButton的背景图像 - Change background image of clicked UIButton created programmatically 按下按钮时如何更改按钮图像 - how to change button image when button pressed 在ios中更改视图的背景图像 - change the background image of a view in ios 单击该单元格中的按钮时如何更改自定义单元格中的标签文本 - how to change label text in custom cell when button in that cell clicked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM