如何在选定值后显示UIPickerView显示标签?

时间:2011-04-27 16:29:49

标签: iphone ios objective-c cocoa-touch

现在,选择器只显示正在选择的数据。我希望它在所选值之后有一个单词,就像iPhone时钟应用程序在选择器上有“小时”和“分钟”一样。

enter image description here

3 个答案:

答案 0 :(得分:3)

在这里,我得到了这个问题的正确答案(我根据你的要求为component改变了一个)看一看: 你需要实现这个方法:

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
   //set your View. Here is an example .. 
UIView *pickerviewtemp=[[UIView alloc] initWithFrame:CGRectZero];

    UILabel *lbl=[[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 50)]autorelease];
    [lbl setBackgroundColor:[UIColor clearColor]];
    [lbl setText:[array_from objectAtIndex:row]];
    [lbl setFont:[UIFont boldSystemFontOfSize:16]];
    [pickerviewtemp addSubview:lbl];


    return pickerviewtemp;

}

在名为int

.h文件中提取lastSelectedIndex个变量
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{  
    UILabel *label = (UILabel*)[pickerView viewWithTag:999+(component*lastSelectedIndex)]; // you can set your own tag...
    [label removeFromSuperview];
    UIView *view = [pickerView viewForRow:row forComponent:component];
    UILabel *lbl2=[[[UILabel alloc] initWithFrame:CGRectMake(110, 0, 50, 50)]autorelease];
    lbl2.tag =  999+(component*row);
    [lbl2 setBackgroundColor:[UIColor clearColor]];
    [lbl2 setText:@yourtext"];
    [lbl2 setFont:[UIFont boldSystemFontOfSize:16]];
    [view addSubview:lbl2];
    lastSelectedIndex = row;
}

我测试了它。希望它会帮助你.... :)

答案 1 :(得分:2)

你必须使用下面的方法自己创建两个标签。这是pickerView的委托方法,当pickerview被加载或者获得reloadcomponent命令时会自动调用。

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

    UIView *pickerviewtemp=[[UIView alloc] initWithFrame:CGRectZero];

    UILabel *lbl=[[[UILabel alloc] initWithFrame:yourrequiredframe]autorelease];
    [lbl setBackgroundColor:[UIColor clearColor]];
    [lbl setText:*set text according to yourself*];
    [lbl setFont:[UIFont boldSystemFontOfSize:16]];
    [pickerviewtemp addSubview:lbl];

    UILabel *lb2=[[[UILabel alloc] initWithFrame:yourrequiredframe]autorelease];
    [lbl2 setBackgroundColor:[UIColor clearColor]];
    [lbl2 setText:*set text according to yourself*];
    [lbl2 setFont:[UIFont boldSystemFontOfSize:16]];
    [pickerviewtemp addSubview:lbl2];


    return pickerviewtemp;
}

答案 2 :(得分:0)

您必须自定义UIPickerView

如果你需要的只是添加一些标签,你可以简单地添加到UIPickerView两个UILabel,因为UIPickerView UIView ...快但又脏......