tags:

views:

1018

answers:

1

I've been searching all over the net for an answer to this one! Hoping one of you can help me.

So far I have a button that produces a random nr between 1 and 6 in a label.

Then I want a picture to appear depending on what nr is in the label.

For example: If the number 1 is generated, I want img1.png to appear in a UIImageView.(?) I'm thinking maybe a if function to pick the picture?

Sorry for bad formating.

Here is the .h file:

#import <UIKit/UIKit.h>

@interface xxxViewController : UIViewController 
{
    IBOutlet  UILabel *label;
    int randomNumber;
}
-(IBAction)randomize;

@end

...and here is the .m file:

#import "xxxViewController.h"
@implementation xxxViewController

-(IBAction)randomize 
{
    int randomNumber = 1+ arc4random() %(6);
    label .text =   [NSString stringWithFormat:@"%d",randomNumber];

}


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];

    int randomNumber = 1+ arc4random() %(6);
    label .text =   [NSString stringWithFormat:@"%d",randomNumber];

}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

@end
+2  A: 

You can use the number to create the file name, for instance [NSString stringWithFormat:@"image%d.png", number];. It's probably a better practice (though not necessary for what you're doing) to create a list of file names, and associate them with numbers in an NSDictionary so you're always dealing with known image names.

Marc Charbonneau
I understand what you are saying, but I'm kinda new to Objective C. Could you maybe implement your first alternative into the code? That would be great!