Search code examples
iosuipickerviewuiactionsheet

Black UIPickerView inside an UIActionSheet


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


Solution

  • 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 !