PDA

View Full Version : How to display a png file based on button name.




busabres31
Feb 29, 2012, 09:31 PM
Newbie here!! I am learning how to code using xCode 4.2. I wanted to create an app that based on the button name in a UITableViewCell it would bring up a .png file. So i have a list of colors, animals, household objects and wanted an image to display based on button pressed. So i have about 300 images and want the image tiger.png to display when the "tiger" button is pressed. The images are full screen so it won't fit on the same view controller (i got that working with smaller images but need to display a full screen image). Any help would be appreciated. Thank you!



jnoxx
Mar 1, 2012, 02:52 AM
Aspect of OOP, you need to create en NSObject, which will contain each "animal".
You can assign property's like Name, ImageName, sound, description, whatever you want.
ANd instead of just showing strings in the tableview, you will show the NSString property of an "Animal" Object.
Otherwise there are several hack n slashes to do it.
One for example, top of my hat, should try to make it work

- (IBAction) clickedThisButton:(id)sender {
UIButton *buttonClicked = (UIButton*)sender;
pointerToNextController.bigImage = [NSString stringWithFormat:@"image_%@", buttonClicked.titleLabel];
/* Do push here */
}

This is very very very dirty, but i just present to you all the solutions possible :)

busabres31
Mar 1, 2012, 06:15 PM
Thank you for taking the time to respond. I am trying to figure out the issue with the code. I am a big time newbie so if you could let me know what the .h and .m files should contain i would appreciate it.

dejo
Mar 1, 2012, 06:32 PM
I am a big time newbie so if you could let me know what the .h and .m files should contain i would appreciate it.

So, you'd like someone to just provide the code for you? Not cool.

busabres31
Mar 1, 2012, 07:24 PM
No, not what I meant, sorry. I am just stuck on this part and can't figure it out. I ordered a Nerd Ranch book and waiting for it to get in and I am getting impatient!!

In the .m file i put the following code:
#import "ViewController.h"
@implementation ViewController
-(IBAction)clickedThisButton : (id)sender {
UIButton *buttonClicked = (UIButton*)sender;
pointerToNextController.bigImage = [NSString stringWithFormat:@"image_%@", buttonClicked.titleLabel];

I put the following in the .h file:
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {
IBOutlet UIViewController *pointerToNextController;
}
-(IBAction)clickedThisButton;
@end

I am getting a property bigImage not found on object of type UIViewController on the and a warning on the @implementation ViewController

dejo
Mar 1, 2012, 09:15 PM
I am getting a property bigImage not found on object of type UIViewController on the and a warning on the @implementation ViewController

Work on fixing these problems first. What makes you believe UIViewController has a bigImage property? And what is the warning? Review your .h. Everything look fine?

jnoxx
Mar 2, 2012, 02:39 AM
I posted pseudo code, you would need to actually change those pieces of code into your environment. So in other words, you need the pointerToOtherController thingy, to be actually a pointer to that controller, not a new UIViewController, and it doesn't have the property bigImage, because that's something I made up and you should add your self to the code.
I think you should be impatient for a while, and grab the book. Because if you are not grasping the basics yet like properties and the pointers, you will write spaghetti code and curse yourself till the end of times in a later stage.
Or get a developer to do it for you.