several UIpickerviews with different pickerView methods?

Discussion in 'iOS Programming' started by 1458279, Jan 28, 2015.

  1. 1458279 Suspended

    1458279

    Joined:
    May 1, 2010
    Location:
    California
    #1
    I'm trying to have to different UIpickerViews with different methods, however every example I see, use the same methods:

    Code:
    - (NSString *)pickerView:(UIPickerView *)picker titleForRow:(NSInteger)row forComponent:(NSInteger)component;
    {
        return [[self.pickerData objectAtIndex:component] objectAtIndex:row];
    }
    
    The closest example I can find, suggest to use the .tag to ID each different pickerView. Then use an "if" inside the method to determine which picker is which.

    The Apple docs say:
    and

    But I can't seem to find any examples of this.

    Can anyone point to an example of a UIPickerView done programmatically that uses a custom UIPickerViewDelegate?

    My guess it would involve changing myPicker.delegate = self; to myPicker.delegate = _____ However I can't seem to find an example in the Apple docs or StackOverflow.

    I probably should have made it clear that I wanted this to not call the standard delegate so that it doesn't change the other pickers.
     
  2. 1458279, Jan 28, 2015
    Last edited: Jan 28, 2015

    1458279 thread starter Suspended

    1458279

    Joined:
    May 1, 2010
    Location:
    California
    #2
    Ok, looks like it must be subclassed (from Stackoverflow):

    Code:
    -(NSInteger)pickerView:(UIPickerView *)pickerView NumberOfRowsInComponent: (NSInteger)component {
      if([pickerView isEqual: agePickerView]){
       return [agePickerArray count];
      }else if([pickerView isEqual: weightPickerView]){
       return [weightPickerArray count];
      }else return 0;
    }
    The problem I see is that he's still calling pickerView, which is no better than this:
    Code:
    weightPickerView.tag = 100; //set the tag
    
    if(pickerView.tag == 100) //check which picker it is
    Ok, looks like he gives a partial example. From what he says, I need to subclass the pickerView methods. Anyone have a complete example of doing this?
     
  3. Punkjumper macrumors member

    Joined:
    Jan 12, 2013
    #3
    what I've done when I have multiple pickers that are displayed inline in a tableView is make a class UIPickerViewDataSource that has the standard methods
    .m file
    Code:
    @implementation UIPickerViewDataSource
    -(instancetype)initWithModelObject:(id)obj{
        self = [super init];
        if (self) {
            
        }
        return self;
    }
    
    -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
        if (!_componentNumber) {
            return 1;
        }else{
            return _componentNumber;
        }
    }
    -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
        return [_pickerDataSource count];
    }
    
    -(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
        
        return [_pickerDataSource objectAtIndex:row];
    }
    - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
        _selectedRow = row;
        [self.delegate pickerDidSelectObject];
        
    }
    -(id)selectedObject{
        return [_dataObjects objectAtIndex:_selectedRow];
    }
    -(id)selectedString{
        return [_pickerDataSource objectAtIndex:_selectedRow];
    }
    
    .h file

    Code:
    @protocol UIPickerDataSourceDelegate<NSObject>
    -(void)pickerDidSelectObject;
    @end;
    
    @interface UIPickerViewDataSource : NSObject <UIPickerViewDataSource, UIPickerViewDelegate>
    @property (retain, nonatomic) id<UIPickerDataSourceDelegate>delegate;
    
    @property (nonatomic, strong)NSArray *pickerDataSource;
    @property(nonatomic, strong)NSArray *dataObjects;
    @property (readwrite, assign)NSUInteger selectedRow;
    @property (nonatomic, assign)NSInteger componentNumber;
    
    //subclass must override
    -(instancetype)initWithModelObject:(id)obj;
    -(id)selectedObject;
    -(id)selectedString;
    @end
    
    @end
    then I would subclass that for each UIPicker and have custom methods that that picker needed to load its data

    Code:
    @implementation TripTitlesPickerDataSource
    
    -(instancetype)initWithModelObject:(id)obj{
        self = [super init];
        if (self) {
            if ([obj isKindOfClass:[Route class]]) {
                _route = obj;
                self.pickerDataSource = [self loadData];
            }
        }
        return self;
    }
    
    -(NSArray *)loadData{
    
    }
    @end
    and finally in the class where I want to use my UIPicker in a tableViewCell I do

    Code:
    -(void)configureCustomPickerCell:(CustomPickerViewCell *)cell atIndexPath:(NSIndexPath *)indexPath{
        UIPickerViewDataSource *source;
        if (pickerIndex == RateIndex) {
            source = _reimbursePickerSource;
        }
        if (pickerIndex == tripNameIndex) {
            source = _tripTitlesPickerSource;
        }
        if (pickerIndex == categoryIndex) {
            source = _categoryPickerDataSource;
        }
        cell.pickerView.delegate = source;
        cell.pickerView.dataSource = source;
        
    
    }
     

Share This Page