3

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):

#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:

'button' undeclared

Am I approaching this the wrong way?

smparkes
  • 13,807
  • 4
  • 36
  • 61
tonyrocks
  • 559
  • 2
  • 10
  • 24

3 Answers3

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:

@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:

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

  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.

Nick Lockwood
  • 40,865
  • 11
  • 112
  • 103
  • A few things you also should consider, try to use image formats like tiff or png. And remove the spaces too. I happened to ran into problems with jpg files before. – Melvin Lai Jan 26 '12 at 02:16
  • Not entirely true. Read this: http://stackoverflow.com/questions/3929281/when-to-use-png-or-jpg-in-iphone-development - basically use PNGs for small images or ones that have transparency, and use JPEGs for large opaque images or photos. – Nick Lockwood Jan 26 '12 at 02:21
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.

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.

dhaya
  • 1,522
  • 13
  • 21
0

You have to write this:

[[self button] setImage:[NSImage imageNamed:@"Costa Rican Frog.jpg"]];
tamasgal
  • 24,826
  • 18
  • 96
  • 135
  • 2
    That's for Mac OS. The question was for iOS. That's also assuming that he's created an outlet for the button, which isn't clear. – Nick Lockwood Jan 26 '12 at 01:08
  • What do yo mean created an outlet? I've only added two things: 1) Button 2) Label above the button. The in the interface builder I drag a connection from the button to the label. – tonyrocks Jan 26 '12 at 01:50
  • What do you mean you connected the button to the label? You should have one IBAction buttonClicked: method, and two IBOutlet @properties on your happydaysViewController class, one that is a UIButton called button and one that is a UILabel called label, and you need to drag these to your button and label respectively in IB. Then you need to drag from your button to your IBAction buttonClicked: method. – Nick Lockwood Jan 26 '12 at 02:24
  • I've updated my answer with the complete code you need for your .h and .m files so you can see if anything is missing. – Nick Lockwood Jan 26 '12 at 02:29
  • Nick, this is incredible. Thanks for so much detail. I was able to get everything working (and then some). Thanks again! – tonyrocks Jan 26 '12 at 13:48