 |
 |
|
 |
02-12-2010, 11:56 AM
|
#1 (permalink)
|
|
Registered Member
Join Date: Feb 2010
Posts: 12
|
Random number to pick an image
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?
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
|
|
|
02-12-2010, 12:12 PM
|
#2 (permalink)
|
|
Obj-C Learner
Join Date: Apr 2009
Location: Manchester, UK
Posts: 1,030
|
Quote:
Originally Posted by @@rz
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?
Here is the .h file:
Code:
#import <UIKit/UIKit.h>
@interface xxxViewController : UIViewController {
IBOutlet UILabel *label;
int randomNumber;
}
-(IBAction)randomize;
@end
...and here is the .m file:
Code:
#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];
if(randomNumber == 1){
// Display Image
}
if(randomNumber == 2){
// Display Image
}
if(randomNumber == 3){
// Display Image
}
if(randomNumber == 4){
// Display Image
}
if(randomNumber == 5){
// Display Image
}
if(randomNumber == 6){
// Display Image
}
}
- (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
|
New code is in bold, it might not be right but it should give you an idea, the proper way would be to use elseif's I think.
|
|
|
02-12-2010, 12:33 PM
|
#3 (permalink)
|
|
Registered Member
Join Date: Feb 2010
Posts: 12
|
What would be the path to the image?
|
|
|
02-12-2010, 01:05 PM
|
#4 (permalink)
|
|
Registered Member
Join Date: Dec 2009
Posts: 271
|
why not use stringWithFormat to gen the image filename?
__________________
@rarindeed
Burstly is the only open and free ad management platform for iPhone app developers.
|
|
|
02-12-2010, 02:36 PM
|
#5 (permalink)
|
|
Registered Member
Join Date: Feb 2010
Posts: 12
|
A guy told me to use this code to create the image filename: [NSString stringWithFormat:@"image%d.png", number];
How do I implement it in the .h and .m ?
|
|
|
03-14-2010, 03:24 PM
|
#6 (permalink)
|
|
Cocoa Junkie
Join Date: Dec 2008
Location: Northern Virginia
Posts: 4,814
|
Getting an image rom disk from an image number
Quote:
Originally Posted by @@rz
A guy told me to use this code to create the image filename: [NSString stringWithFormat:@"image%d.png", number];
How do I implement it in the .h and .m ?
|
Like this:
Code:
UIImage* imageForNumber: (NSInteger) image_number
{
NSString filename = [NSString stringWithFormat: @image%d.png", image_number];
UIImage* theImage = [UIImage imageNamed: filename];
return theImage;
}
The imageNamed method will look for the image in your app's main bundle (images built into your program.)
The code above will return nil if there is no image with the specified filename.
Regards,
Duncan C
|
|
|
 |
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
» Advertisements |
» Online Users: 273 |
| 20 members and 253 guests |
| ADY, AragornSG, Bertrand21, Dani77, Dattee, Duncan C, fkmtc, HDshot, HemiMG, iDifferent, JasonR, macquitzon216, mer10, prchn4christ, Rudy, sacha1996, sneaky, spiderguy84, Sunny46, theone8one |
| Most users ever online was 1,187, 10-11-2011 at 08:09 AM. |
» Stats |
Members: 158,885
Threads: 89,231
Posts: 380,768
Top Poster: BrianSlick (7,129)
|
| Welcome to our newest member, bookesp |
|