0

To preface this question, I am extremely new to Objective-C and Xcode. I have only been learning for a week or so now but I have been stuck on this problem for a few days. I am trying to display an image in a UIButton. I have 3 to choose from, blue.png, purple.png and orange.png. No matter what I do or what image I tell Xcode to use, it will only display the blue image. I have deleted the image from the project entirely. I have "reset content and settings" in the iOS simulator. What exactly is going on here?

the.h code

IBOutlet UIButton *h1;

the.m code

UIImage *buttonImage = [UIImage imageNamed:@"purple.png"];
[h1 setImage:buttonImage forState:UIControlStateNormal];
[self.view addSubview:h1];

No matter which image I specify within the quotes, it always uses blue.png, even when the image no longer exists in the project. Thanks for the help everybody!

Logan
  • 52,262
  • 20
  • 99
  • 128
Sev
  • 883
  • 1
  • 14
  • 34
  • Are you able to get your images to display elsewhere? Have you made sure your image isn't nil? – Logan Apr 14 '14 at 15:07
  • I know the image is working because I am displaying a background image also. And if I change the code to have the image display on another button, then that button will just display the blue image. I will use a switch and a random to have the buttons display each image randomly and they all just turn out blue. I am also using the orange.png to color an ImageView and it works fine. – Sev Apr 14 '14 at 15:13

2 Answers2

2

You must have set blue.png as the background image in the storyboard or xib file. Write NSLog(@"%@",buttonImage) to make sure that the image is not nil. Make sure that you are entering the correct name and extension purple.png

And also, use

[h1 setBackgroundImage:buttonImage forState:UIControlStateNormal];

instead of setImage

Khawar Ali
  • 3,462
  • 4
  • 27
  • 55
  • I will give this a try and report back. – Sev Apr 14 '14 at 15:15
  • That worked to get the images to update and display correctly! Can someone explain why xcode was using an image that did not exist as the default? I also had to attempt to change the image of the button before the default would display. I really do want to understand xcode better, it is very different from the other IDE's I have used. Thanks again, yall help me out so much every time I need it. You rock! – Sev Apr 14 '14 at 15:22
1

I think first of all you should know the difference button.image and button.backgroundImage.

Using the Image (currentImage) field, you can specify an image to appear within the content of your button. If the button has a title, this image appears to the left of it, and centered otherwise.

The Background (currentBackgroundImage) field allows you to specify an image to appear behind button content and fill the entire frame of the button.

So you can use setImage or setBackgroundImage as what you want.

andyleehao
  • 2,412
  • 1
  • 15
  • 8