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

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

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

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 07-08-2010, 11:23 AM   #1 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 9
Freshwreckage is on a distinguished road
Default Randomized arrays

Hey all...

I am working with another partner who has taken a sudden sabbatical and left me with a task that is a might bit beyond me, but certainly not something that I am afraid to tackle... I have an app that uses 4 arrays. The interface is basically 4 picker wheels that one can use to select from a plethora of choices to form a 4 part answer.

But, I need to randomize these not unlike what urban spoon does short of the graphical aspect.

I just need to implement a button that will reach into the arrays and give me a random selection from each array.

Here is my code as it sits now...

Code:
import "DoubleComponentPickerViewController.h"

@implementation DoubleComponentPickerViewController

@synthesize doublePicker;
@synthesize firstTypes;
@synthesize middleTypes;
@synthesize lastTypes;

-(IBAction)buttonPressed
{
	NSInteger firstRow = [doublePicker selectedRowInComponent:kfirstComponent];
	NSInteger middleRow = [doublePicker selectedRowInComponent:kmiddleComponent];
	NSInteger lastRow = [doublePicker selectedRowInComponent:klastComponent];
	NSString *first = [firstTypes objectAtIndex:firstRow];
	NSString *middle = [middleTypes objectAtIndex:middleRow];
	NSString *last = [lastTypes objectAtIndex:lastRow];
	NSString *message = [[NSString alloc] initWithFormat:@"%@ %@ %@ test.",first, middle, last];
	UIAlertView *alert = [[ UIAlertView alloc] initWithTitle:@"Description:"
													 message:message
													delegate:nil
										   cancelButtonTitle:@"Cancel"
										   otherButtonTitles:nil];
	[alert show];
	[alert release];
	[message release];
}


- (void)viewDidLoad 
{
	NSArray *firstArray = [[NSArray alloc] initWithObjects:
			@"a",@"b",@"c",@"d",nil];
		self.firstTypes = firstArray;
		[firstArray release];
	
		NSArray *middleArray = [[NSArray alloc] initWithObjects:
			@"A",@"B",@"C",@"D",nil];
		self.middleTypes = middleArray;
		[middleArray release];
		
		NSArray *lastArray = [[NSArray alloc] initWithObjects:
			@"1",@"2",@"3",@"4",nil];
		self.lastTypes = lastArray;
		[lastArray release];
	
	
	[super viewDidLoad];
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
	return 3;
}


/* Ugly attempt at sizing font */
/*

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view 
{
	UILabel *pickerLabel = (UILabel *)view;
	// Reuse the label if possible, otherwise create and configure a new one
	if ((pickerLabel == nil) || ([pickerLabel class] != [UILabel class])) 
	{  //newlabel
		CGRect frame = CGRectMake(0.0, 0.0, 270, 32.0);
		pickerLabel = [[[UILabel alloc] initWithFrame:frame] autorelease];
		pickerLabel.textAlignment = UITextAlignmentLeft;
		pickerLabel.backgroundColor = [UIColor clearColor];
		pickerLabel.font = [UIFont fontWithName:@"AmericanTypewriter" size:8];
	}			
	pickerLabel.textColor = [UIColor blackColor];
	return pickerLabel;	
}

*/


-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
	if (component == 0) 
	{
		return [self.firstTypes count];
	}
	
	if (component == 1) 
	{
		return [self.middleTypes count];
	}
	
	else 
	{
		return [self.lastTypes count];
	}
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view 
{
	UILabel *pickerLabel = (UILabel *)view;
	// Reuse the label if possible, otherwise create and configure a new one
	if ((pickerLabel == nil) || ([pickerLabel class] != [UILabel class])) 
	{  //newlabel
		CGRect frame = CGRectMake(0.0, 0.0, 270, 32.0);
		pickerLabel = [[[UILabel alloc] initWithFrame:frame] autorelease];
		pickerLabel.textAlignment = UITextAlignmentLeft;
		pickerLabel.backgroundColor = [UIColor clearColor];
		pickerLabel.font = [UIFont fontWithName:@"AmericanTypewriter" size:8];
	}			
	pickerLabel.textColor = [UIColor blackColor];
	NSString*  text = nil;
	
	if (component == 0) 
	{
		text =  [self.firstTypes objectAtIndex:row];
	}
	else
		if (component == 1) 
		{
			text =  [self.middleTypes objectAtIndex:row];
		}
		else 
		{
			text =   [self.lastTypes objectAtIndex:row];
		}
	
	pickerLabel.text = text;
	
	return pickerLabel;	
}

/*
-(NSString *)pickerView:(UIPickerView *)pickerView
			titleForRow:(NSInteger)row forComponent:(NSInteger)component 
{
	if (component == 0) 
	{
		return [self.firstTypes objectAtIndex:row];
	}
	
	if (component == 1) 
	{
		return [self.middleTypes objectAtIndex:row];
	}
	
	else 
	{
		return [self.lastTypes objectAtIndex:row];
	}
	
	
}
*/

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

- (void)viewDidUnload 
{
}


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

@end
It works fine manually, but I want a randomize function and I am not sure where to start. Any help would be appreciated.

Thank you in advance.
Freshwreckage is offline   Reply With Quote
Old 07-08-2010, 12:21 PM   #2 (permalink)
Registered Member
 
Glnn's Avatar
 
Join Date: Jun 2009
Location: Netherlands
Posts: 31
Glnn is on a distinguished road
Default

You could generate a random number between 0 and the numbers of objects in your array.
And use those numbers to addObjectAtIndex in a new array.

Good idea?
Glnn is offline   Reply With Quote
Old 07-08-2010, 02:28 PM   #3 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 9
Freshwreckage is on a distinguished road
Default

Quote:
Originally Posted by Glnn View Post
You could generate a random number between 0 and the numbers of objects in your array.
And use those numbers to addObjectAtIndex in a new array.

Good idea?
Sounds good... Just unsure of the implementation. Like I said... Main guy has gone on an instant sabbatical. Unsure of when he will return.
Freshwreckage is offline   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: 336
10 members and 326 guests
cgokey, EXOPTENDAELAX, GHuebner, Mirotion22, ohmniac, PavelSea, Pudding, SLIC, Sloshmonster, Sonuye857
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,653
Threads: 94,115
Posts: 402,888
Top Poster: BrianSlick (7,990)
Welcome to our newest member, ohmniac
Powered by vBadvanced CMPS v3.1.0

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