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 > iPhone SDK Development - Advanced Discussion

Reply
 
LinkBack Thread Tools Display Modes
Old 06-02-2010, 01:52 PM   #1 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 9
Freshwreckage is on a distinguished road
Default 3 column picker blues

Hey all...

I am pulling my hair out on this one and for those who know this, it may be simple...

I am working on setting up an app that has three pickers. The backend seems to work as it returns the requested data. But, while wheel 1 and 2 have the requisite info, wheel 3 is visually identical to wheel 2. Yet when I hit the button, it is still pulling the proper info from the array.

Any help would be appreciated.

Thank you in advance...

Here is my code:

(Header file .h)
Code:
#import <UIKit/UIKit.h>
#define kfirstComponent 0	
#define kmiddleComponent 1
#define klastComponent 2

@interface DoubleComponentPickerViewController : UIViewController
	<UIPickerViewDelegate, UIPickerViewDataSource>
	{
		IBOutlet UIPickerView *doublePicker;
			NSArray *firstTypes;
			NSArray *middleTypes;
			NSArray *lastTypes;	
	}

@property (nonatomic,retain)  UIPickerView *doublePicker;
@property (nonatomic,retain)  NSArray *firstTypes;
@property (nonatomic,retain)  NSArray *middleTypes;
@property (nonatomic,retain)  NSArray *lastTypes;

-(IBAction)buttonPressed;



@end

(Config file .M)



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:@"Your name is: %@ %@ %@.",first, middle, last];
		UIAlertView *alert = [[ UIAlertView alloc] initWithTitle:@"Full name"
			message:message
			delegate:nil
			cancelButtonTitle:@"Cancel"
			otherButtonTitles:nil];
		[alert show];
		[alert release];
		[message release];
	}

- (void)viewDidLoad 
	{
		NSArray *firstArray = [[NSArray alloc] initWithObjects:
			@"Willy",@"Peter",@"Michael",@"Sam",@"Bobby",nil];
		self.firstTypes = firstArray;
		[firstArray release];
	
		NSArray *middleArray = [[NSArray alloc] initWithObjects:
			@"Nathaniel",@"Joseph",@"Tad",@"Chad",@"Ross",nil];
		self.middleTypes = middleArray;
		[middleArray release];
		
		NSArray *lastArray = [[NSArray alloc] initWithObjects:
			 @"Jones",@"Pearson",@"Parker",@"Hart",@"Andersen",nil];
		self.lastTypes = lastArray;
		[lastArray release];

		
		[super viewDidLoad];
	}

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

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

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

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

- (void)viewDidUnload 
	{
	}


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

@end
Thank you again...


FW
Freshwreckage is offline   Reply With Quote
Old 06-02-2010, 02:06 PM   #2 (permalink)
A Single-Serving Friend
 
Join Date: Mar 2010
Location: Groningen, NL
Posts: 491
Robert Paulson is on a distinguished road
Default

Hi,

from your description, I am not sure I understand what's going wrong. However, there most certainly is something wrong here:

Code:
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
		if (component == kfirstComponent)
			return[self.firstTypes count];
			return[self.middleTypes count];
			return[self.lastTypes count];
}
This doesn't make sense at all. What you want to do is this:

Code:
if (component == 0) {
	return [self.firstTypes count];
}

if (component == 1) {
	return [self.middleTypes count];
}

else {
	return [self.lastTypes count];
}
The same applies to your pickerView:titleForRow:forComponent: method. Google for a tutorial on if-statements to see the syntax you need in case you don't understand my code. Alternatively, you could use a switch-statement (I personally prefer that) but I thought it's best to stay as close to your code as possible.

Hope this helps.

Cheers,
Bob
__________________
We are God’s middle children, according to Tyler Durden, with no special place in history and no special attention.

Consider saying thanks by buying my app. :]
Robert Paulson is offline   Reply With Quote
Old 06-02-2010, 06:57 PM   #3 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 9
Freshwreckage is on a distinguished road
Default

Hey there...

Thanks!

Worked like a charm. Now I can put my hair back.
One more question if you don't mind...

Not finding anything in IB to manipulate the fornt style for the picker in IB.

Any pointers on a good place to look for that?

Thank you.



Quote:
Originally Posted by Robert Paulson View Post
Hi,

from your description, I am not sure I understand what's going wrong. However, there most certainly is something wrong here:

Code:
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
		if (component == kfirstComponent)
			return[self.firstTypes count];
			return[self.middleTypes count];
			return[self.lastTypes count];
}
This doesn't make sense at all. What you want to do is this:

Code:
if (component == 0) {
	return [self.firstTypes count];
}

if (component == 1) {
	return [self.middleTypes count];
}

else {
	return [self.lastTypes count];
}
The same applies to your pickerView:titleForRow:forComponent: method. Google for a tutorial on if-statements to see the syntax you need in case you don't understand my code. Alternatively, you could use a switch-statement (I personally prefer that) but I thought it's best to stay as close to your code as possible.

Hope this helps.

Cheers,
Bob
Freshwreckage is offline   Reply With Quote
Old 06-03-2010, 09:16 AM   #4 (permalink)
A Single-Serving Friend
 
Join Date: Mar 2010
Location: Groningen, NL
Posts: 491
Robert Paulson is on a distinguished road
Default

Hi,

no, I don't mind. That's what this place is for.

Front style... you mean like the look of the picker? It's color etc.? As far as I know, you cannot change that. I wanted to do that myself the other day and haven't found something on it. The only 'solution' I found was some people suggesting to take a screen shot of the picker, customize it in Photoshop and then place the customized image over the real picker.

That's what I'll do since I haven't come across any better solution. Let me know if you find a way!

Hope this helps.

Cheers,
Bob
__________________
We are God’s middle children, according to Tyler Durden, with no special place in history and no special attention.

Consider saying thanks by buying my app. :]
Robert Paulson is offline   Reply With Quote
Old 06-03-2010, 10:45 AM   #5 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 9
Freshwreckage is on a distinguished road
Default

Sorry...

I fat fingered the word "font".

I want to change the font size and type.

I knew I should have paid closer attention in typing class...


Quote:
Originally Posted by Robert Paulson View Post
Hi,

no, I don't mind. That's what this place is for.

Front style... you mean like the look of the picker? It's color etc.? As far as I know, you cannot change that. I wanted to do that myself the other day and haven't found something on it. The only 'solution' I found was some people suggesting to take a screen shot of the picker, customize it in Photoshop and then place the customized image over the real picker.

That's what I'll do since I haven't come across any better solution. Let me know if you find a way!

Hope this helps.

Cheers,
Bob
Freshwreckage is offline   Reply With Quote
Old 06-03-2010, 10:52 AM   #6 (permalink)
A Single-Serving Friend
 
Join Date: Mar 2010
Location: Groningen, NL
Posts: 491
Robert Paulson is on a distinguished road
Default

Hey,

check out this thread over at Stack Overflow. Does that help?

Cheers,
Bob
__________________
We are God’s middle children, according to Tyler Durden, with no special place in history and no special attention.

Consider saying thanks by buying my app. :]
Robert Paulson is offline   Reply With Quote
Old 06-08-2010, 07:02 PM   #7 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 9
Freshwreckage is on a distinguished road
Default

Well, my first run is not necessarily producing the results I want heh... Unless of course I want blank wheels. Nope.

If I had hair I would have pulled it all out by now. You would think I would be getting this by now.

Here's my code.

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
Any help in the right direction would be appreciated.

Thank you.
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: 362
6 members and 356 guests
.Snipe, AragornSG, baja_yu, guusleijsten, NSString, Paul Slocum
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,645
Threads: 94,111
Posts: 402,862
Top Poster: BrianSlick (7,990)
Welcome to our newest member, leighec68
Powered by vBadvanced CMPS v3.1.0

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