iphone - 如何让我的 UIPickerView 在选定值之后显示标签?

标签 iphone ios objective-c cocoa-touch

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

enter image description here

最佳答案

在这里,我得到了这个问题的正确答案(我已经为一个 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;

}

.h 文件中取一个 int 变量命名为 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;
}

我已经测试过了。希望它能帮助你.... :)

关于iphone - 如何让我的 UIPickerView 在选定值之后显示标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5807411/

相关文章:

ios - 进行更改后,Xcode Storyboard不会刷新

iphone - IOS 4.0 代码不在 IOS 4.2 上运行

ios - 以编程方式设置 EKEvent 标题

ios - 无法从 MapKitView 转到 Controller

iphone - iOS 静态单元格、自动滚动和附加工具栏键盘

iphone - iPhone 应用而非设备的唯一标识符

ios - Objective-C iPhone - 立即在 Safari 中打开 URL 并退出应用程序

iphone - 从对象 init 的 GCD 安全地调用后面的 UIViewController 方法

ios - IB_DESIGNABLE 使 Xcode 在离开 Storyboard时停止响应

iphone - 在 Progress View 上绘制图像