开发者

Random number to pick an image Xcode iPhone

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)ran开发者_运维百科domize;

@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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜