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

时间:2022-11-21 21:11:43

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. In fact, I get the following error:

因此,标签文本从空白变为pop tart,但是按钮的背景图像并没有从空白变为Costa Rican Frog.jpg。事实上,我得到了以下错误:

'button' undeclared

“按钮”未申报

Am I approaching this the wrong way?

我是不是走错路了?

3 个解决方案

#1


4  

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. 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". 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.

    错误你表明你没有得到所谓的按钮,可能因为你不添加到您的类,也可能因为你叫IBAction方法“按钮”,这是相互矛盾的属性的getter方法称为按钮,这也被称为“按钮”。在我的代码我按钮作为一个参数传递给点方法(你需要re-bind IB的按钮如果你改变它的名字),一个)提供了另一种方式的按钮和b)变化的方法的名称,所以它不会冲突如果你想有一个所谓的按钮在你的视图控制器。

#2


0  

You have to write this:

你必须这样写:

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

#3


0  

"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,它将显示在touch up上按钮的变化。

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.

在运行应用程序时,您将发现它会修改映像。

#1


4  

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. 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". 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.

    错误你表明你没有得到所谓的按钮,可能因为你不添加到您的类,也可能因为你叫IBAction方法“按钮”,这是相互矛盾的属性的getter方法称为按钮,这也被称为“按钮”。在我的代码我按钮作为一个参数传递给点方法(你需要re-bind IB的按钮如果你改变它的名字),一个)提供了另一种方式的按钮和b)变化的方法的名称,所以它不会冲突如果你想有一个所谓的按钮在你的视图控制器。

#2


0  

You have to write this:

你必须这样写:

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

#3


0  

"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,它将显示在touch up上按钮的变化。

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.

在运行应用程序时,您将发现它会修改映像。