Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development > iPhone SDK Development - Advanced Discussion

Reply
 
LinkBack Thread Tools Display Modes
Old 02-12-2010, 11:56 AM   #1 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 12
Default 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
@@rz is offline   Reply With Quote
Old 02-12-2010, 12:12 PM   #2 (permalink)
Obj-C Learner
 
Join Date: Apr 2009
Location: Manchester, UK
Posts: 1,030
Send a message via MSN to ZunePod Send a message via Yahoo to ZunePod
Default

Quote:
Originally Posted by @@rz View Post
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.
ZunePod is offline   Reply With Quote
Old 02-12-2010, 12:33 PM   #3 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 12
Default

What would be the path to the image?
@@rz is offline   Reply With Quote
Old 02-12-2010, 01:05 PM   #4 (permalink)
Registered Member
 
Join Date: Dec 2009
Posts: 271
Default

why not use stringWithFormat to gen the image filename?
__________________
@rarindeed


Burstly is the only open and free ad management platform for iPhone app developers.
rarindeed is offline   Reply With Quote
Old 02-12-2010, 02:36 PM   #5 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 12
Default

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 ?
@@rz is offline   Reply With Quote
Old 03-14-2010, 03:24 PM   #6 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 4,814
Default Getting an image rom disk from an image number

Quote:
Originally Posted by @@rz View Post
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
Duncan C is online now   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» 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
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:10 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0