3

I'am trying to add a UIPicker View inside a UIActionSheet ! The delegate and datasource for the picker are set correctly ! (I tested it separately without actionsheet) But when the actionsheet show in the view the pickerview is horrible black box ! I searched for the problem and I hear the the datasource and delegate of the UIPickerView are not set correctly ! I thought that the picker view dont call its method when it's embaded inside a uiactionsheet ! so I forced the picker to reload all its compomnent will the actionsheet

EDIT : I make the correction and the code work perfectly !

- (void) presentPicker{
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Select some value" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Select", nil];
    actionSheet.tag = AS_VIEW_TAG;

    UIPickerView* picker = nil;
    picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 40, 320, 216)];
    picker.tag = 90;
    picker.dataSource = self;
    picker.delegate = self;
    [picker setShowsSelectionIndicator:YES];

    [actionSheet addSubview:picker];
    [actionSheet showInView:self.view];
    [actionSheet setFrame:CGRectMake(0, 50, 320, 380)];
}

- (void) willPresentActionSheet:(UIActionSheet *)actionSheet{
    if(actionSheet.tag == AS_VIEW_TAG){
        
        UIPickerView* picker = [actionSheet viewWithTag:PICKER_TAG];
        //I force reload 
        [picker reloadAllComponents];
        NSArray *subviews = [actionSheet subviews];
        [[subviews objectAtIndex:1] setFrame:CGRectMake(20, 266, 280, 46)];
        [[subviews objectAtIndex:2] setFrame:CGRectMake(20, 317, 280, 46)];
    }
}

UIPicker Delegate/Datasource Methodes

- (NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    if (pickerView.tag == 90) {
        return 1;
    }
    else{
        return 0;
    }
    
}

- (NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if(pickerView.tag == 90){
        return 20;
     }
     else{
         return 0;
     }
}

- (UIView*) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 37)];
    label.text = [NSString stringWithFormat:@"Value %d", ];
    label.textAlignment = UITextAlignmentCenter;
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont boldSystemFontOfSize:17];
    return label;
}

Here is the result :

black pickerview inside actionsheet

Community
  • 1
  • 1
Aladdin Gallas
  • 701
  • 2
  • 12
  • 36

3 Answers3

0

I think adding pickerview inside action sheet may be a mess there is a good workaround for this one Check this SO question

Community
  • 1
  • 1
Lithu T.V
  • 19,955
  • 12
  • 56
  • 101
  • Yeah ... but I already tried the UIDatePicker inside the actionsheet and it was a nice result ! Why it's doing that for the UIPickerView ! I think I have to try subclassing the UIPickerView maybe it's will do the desired result ! – Aladdin Gallas Oct 05 '12 at 15:10
0

One of my projects also has a UIPicker as a subview of UIActionSheet and it works fine. You do not need to force reload it unless something has changed. Try removing this entire block:

[picker reloadAllComponents];
NSArray *subviews = [actionSheet subviews];
[[subviews objectAtIndex:1] setFrame:CGRectMake(20, 266, 280, 46)];
[[subviews objectAtIndex:2] setFrame:CGRectMake(20, 317, 280, 46)];

Not sure what you are trying to do with those last two lines but I suspect it might be the problem.

Edwin Iskandar
  • 4,119
  • 1
  • 23
  • 24
0

I did it wrong ...

I changed (I don't know why) the picker tag and then the message :

- (NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView;

returned a 0

So I have just to delete :

    [picker setTag:PICKER_TAG]

and it will work perfectly !

Aladdin Gallas
  • 701
  • 2
  • 12
  • 36