views:

55

answers:

1

I am using two UIPickers in a view. first picker has 3 components and second has one. but when I select items, it shows correct items from first picker but always return first item from second picker regardless of selected row. Please help.

here is the code I am using.

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
   if (pickerView == triplePicker) 
     return 3;
   else {
     return 1;
   }
}


-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
   if (pickerView == triplePicker) {  
      if (component == kColorComponent)
         return[colorList count];
      if (component == kClarityComponent)
         return[clarityList count];
      return[shapeList count];
   }
   else{
      return [listPickerItems count];
   }
}

-(NSString *)pickerView:(UIPickerView *)pickerView
            titleForRow:(NSInteger)row
           forComponent:(NSInteger)component 
{   
   if (pickerView == triplePicker) {  
      if (component == kColorComponent)
         return [colorList objectAtIndex:row];
      if (component == kClarityComponent)
         return [clarityList objectAtIndex:row];
      return [shapeList objectAtIndex:row];  
   }
   else{
      return [listPickerItems objectAtIndex:row];
   }
}

in buttonpressed event I have following for second picker to return the item selected:

NSInteger pickrow = [listPicker selectedRowInComponent:0];
NSString *picked = [listPickerItems objectAtIndex:pickrow];
A: 

I would definitely suggest using two separate delegates to handle two pickers.

Aside from that, I'm going to guess that you're using Interface Builder to set up your view. If that's the case check if you have properly linked your listPicker with your File's Owner. If listPicker would be nil, then selectedRowInComponent: would always return null (0 for NSInteger), hence would always select the first item in your array.

EDIT: Some sample code for separate delegates:

You need to create a second class to be your delegate, like this:

FirstPickerDelegate.h

#import <Foundation/Foundation.h>

@class UntitledViewController;

@interface FirstPickerViewDelegate : NSObject <UIPickerViewDelegate, UIPickerViewDataSource> {
    NSArray* values;
    IBOutlet UntitledViewController* viewController;
}
-(void) loadData;

@property (nonatomic, retain) NSArray* values;
@property (nonatomic, assign) IBOutlet UntitledViewController* viewController;

@end

FirstPickerViewDelegate.m

#import "FirstPickerViewDelegate.h"


@implementation FirstPickerViewDelegate
@synthesize values;
@synthesize viewController;

-(id) init
{
    if ( self = [super init] )
    {
        [self loadData];
    }
    return self;
}

-(void) loadData
{
    NSArray* array = [[NSArray alloc] initWithObjects:@"first", @"second", @"third", nil];
    self.values = array;
    [array release];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [values count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [values objectAtIndex:row];
}

-(void) dealloc
{
    self.values = nil;
    [super dealloc];
}
@end

Your ViewController (UntitledViewController here, sorry about the name):

#import <UIKit/UIKit.h>

@class FirstPickerViewDelegate;
@interface UntitledViewController : UIViewController {
    IBOutlet UIPickerView* firstPicker;
    IBOutlet FirstPickerViewDelegate* firstDelegate;
}

@property (nonatomic, retain) IBOutlet UIPickerView* firstPicker;
@property (nonatomic, retain) IBOutlet FirstPickerViewDelegate* firstDelegate;

@end

Basically you need to drop an NSObject on your object list, and change it's class to FirstPickerViewDelegate, then make connections like this: alt text

I feel I overelaborated this time, but I'm in a good mood today so whatever :P

About the main question: double check that listPicker is not nil at the time of pressing the button, if it is not, try to use

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

to track down the error.

Estarriol
I am not sure how to use two separate delegates for two pickers.my listPicker(second picker) is properly linked to File's Owner in IB.
ashp
Added some sample code for you. Hope that helps.
Estarriol
oops....my mistake.Thanks for all your help.Looks like I didn't make proper connection from File's Owner to the listPicker. It is now returning the correct value for the selected row.
ashp
Glad I could help :)
Estarriol