3

我正在尝试在 UIActionSheet 中添加 UIPicker 视图!选择器的委托和数据源设置正确!(我在没有操作表的情况下单独测试了它)但是当操作表显示在视图中时,pickerview 是可怕的黑框!我搜索了问题,我听说 UIPickerView 的数据源和委托设置不正确!我认为选择器视图不会调用它的方法,当它在一个UIActionsheet内浮现!所以我强迫选择器重新加载其所有组件将操作表

编辑:我进行了更正,代码完美运行!

- (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 委托/数据源方法

- (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;
}

这是结果:

动作表内的黑色pickerview

4

3 回答 3

0

我认为在操作表中添加pickerview可能会一团糟,对此有一个很好的解决方法检查这个SO问题

于 2012-10-05T15:03:49.500 回答
0

我的一个项目也有一个 UIPicker 作为 UIActionSheet 的子视图,它工作正常。除非发生了变化,否则您无需强制重新加载它。尝试删除整个块:

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

不确定您要对最后两行做什么,但我怀疑这可能是问题所在。

于 2012-10-05T15:27:07.283 回答
0

我做错了...

我改变了(我不知道为什么)选择器标签,然后是消息:

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

返回一个 0

所以我只需要删除:

    [picker setTag:PICKER_TAG]

它会完美地工作!

于 2012-10-05T18:40:34.760 回答