如何将uipickerview作为子视图添加到iPhone中的主视图?

问题描述

| 我有一个应用程序,当用户单击一个按钮时,我想打开uipickerview作为主视图的子视图,并在用户选择一个项目后将其从主视图中删除(下拉功能),为此我编写了代码如下:
-(void)showPrefPicker:(id)sender
{


   UIView *subView=[[UIView alloc] init];
   subView.frame=CGRectMake(180,120,150,150);
   pickerView = [[UIPickerView alloc] init];

    pickerView.showsSelectionIndicator = YES;
    pickerView.dataSource = self;
    pickerView.delegate = self;
    pickerView.frame=CGRectMake(190,130,100,100);
    subView.backgroundColor=[UIColor blackColor];
    [subView  addSubview:pickerView];
    [self.view addSubview:subView];
    [pickerView release];

}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
    return 1;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    NSLog(@\"selected object is %@\",[arraypref objectAtIndex:row]);
    //[pickerView ]
    //mlabel.text=  [arrayNo objectAtIndex:row];
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
    return [arraypref count];
}

- (Nsstring *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
    return [arraypref objectAtIndex:row];
}
但是只在主视图中显示子视图而不是pickerview。我该怎么做任何教程或源代码?     

解决方法

更好地使用 隐藏
pickerview.hidden=YES;
显示
pickerview.hidden=NO;
    ,尝试将
pickerView
中的
frame
更改为
pickerView.frame = CGRectMake(10,10,100,100);
    ,用你的代码
subView.frame=CGRectMake(180,120,150,150);
pickerView.frame=CGRectMake(190,130,100);
选择器视图是您的\'subView \'的子视图 在这里,pickerview的框架开始于子视图的边界之外,即原点x为190。但是子视图的宽度仅为150。 所以正确的代码是
pickerView.frame=CGRectMake(0,100);
因为pickerview是自定义视图的子视图。它应该在其父视图的框架中。