创建UIPickerView。委托=自

时间:2011-12-13 00:55:15

标签: objective-c

- (void)viewDidLoad {
    [super viewDidLoad];

    UIView *myView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 250, 100, 100)];

    [myView1 setBackgroundColor:[UIColor blueColor]];

    [self.view addSubview:myView1];

    UIPickerView *pickerView1 = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 100.0)];
    pickerView1.delegate = self;
    pickerView1.tag = kVIEW1_PICKERVIEW_TAG;
    pickerView1.showsSelectionIndicator = YES;
    [myView1 addSubview:pickerView1];

    table =[[NSMutableArray alloc]init];
    [table addObject:@"Giraffe"];
    [table addObject:@"Water Bottle"];
    [table addObject:@"Sprinning Fan"];
    [table addObject:@"Mairy Monkey"];

    label =[[UILabel alloc] initWithFrame:CGRectMake(100, 120, 130, 130)];
    label.backgroundColor=[UIColor clearColor];
    label.text =@"Label Page";
    label.textAlignment = UITextAlignmentCenter;
    [self.view addSubview:label];

}

1 个答案:

答案 0 :(得分:3)

一些问题。您已使用错误的名称实现了两个方法:-pickView:didSelectRow:inComponent:应为-pickerView:didSelectRow:inComponent:-pickView:titleForRow:forComponent:应为-pickerView:titleForRow:forComponent:。此外,您正在设置选择器视图的delegate,但您没有设置其dataSource,因此您的代码将返回要显示的实际项目未被调用;你还需要一个pickerView1.dataSource = self;

另外,正如sosborn和Rickay评论的那样,你的帖子真的不清楚 - 我能回答这个问题的唯一原因是因为你发布的代码显然有些问题。一般来说,要在SO上获得可用的答案,您需要提供更多信息:您正在尝试做什么,确切地说无法正常工作,以及(理想情况下)您到目前为止尝试解决的问题。提出一个实际问题也有帮助。