Search code examples
ioscocoa-touchuipickerview

How do I use viewForRow and titleForRow in same class for different pickers?


I have 3 pickers on my view, one of which contains only images.

So, for this one, I have to use viewForRow, but for the other 2, which are common pickers with labels, I need titleForRow, except that if I use viewForRow, titleForRow never gets called.

How can I have both delegates called in my class?

This is what I have:

- (UIView *)pickerView:(UIPickerView *)thePickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{

    [[[objectPicker subviews] objectAtIndex:0] setHidden:YES];
    [[[objectPicker subviews] objectAtIndex:5] setHidden:YES]; 
    [[[objectPicker subviews] objectAtIndex:8] setHidden:YES];

    return ; //will be implemented, but this will return the icons
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    [[[objectPicker subviews] objectAtIndex:0] setHidden:YES];
    [[[objectPicker subviews] objectAtIndex:5] setHidden:YES];
    [[[objectPicker subviews] objectAtIndex:8] setHidden:YES]; 

    if (pickerView == self.timePicker) {
        return [timeList objectAtIndex:row];
    }
    else if(pickerView == self.powerPicker) {
        return [powerList objectAtIndex:row];
    }
}

Solution

  • Created different classes for each of the pickers, each one containing their own delegates. Then simply imported them into my mainController class and initialized them in the viewDidLoad method.

    Here is the solution I usedL

    ===========================Found the solution(workaround):===========================

    First, I created 3 different classes for my 3 pickers, each one with a different .xib. Added these 3 pickers into my main controller class:

    //Don't forget #import "YourClass.h" and instantiate it on the .h file of your main controller

    objPickerVC = [[ObjectPickerViewController alloc] initWithNibName:@"ObjectPickerViewController" bundle:[NSBundle mainBundle]];
    powerPickerVC = [[PowerPickerViewController alloc] initWithNibName:@"PowerPickerViewController" bundle:[NSBundle mainBundle]];
    timePickerVC = [[TimePickerViewController alloc] initWithNibName:@"TimePickerViewController" bundle:[NSBundle mainBundle]];
    
    CGRect rect1 = CGRectMake(5, 43, 100, 216); 
    objPickerVC.view.frame = rect1;
    [self.view addSubview:objPickerVC.view];
    
    CGRect rect2 = CGRectMake(110, 43, 100, 216); 
    powerPickerVC.view.frame = rect2;
    [self.view addSubview:powerPickerVC.view];
    
    CGRect rect3 = CGRectMake(215, 43, 100, 216);
    timePickerVC.view.frame = rect3;
    [self.view addSubview:timePickerVC.view];
    

    By doing that, I have individual delegates for each pickerView allowing me to set them however I need them to be.

    P.S: It's important, for positioning matters, that on your pickerViews xib you centralize 'em on the view. If you don't, when you try to position them -> CGRectMake(215, 43...) for example, will not be on the REAL (215, 43...) position. I know, it doesn't make any sense, but it's an awful bug and makes you have to position it on trials. Change value - run - change - run, until you get it right.