1

我有一个应用程序,当用户单击一个按钮时,我想打开 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。我该怎么做任何教程或源代码?

4

3 回答 3

1

更好地使用

隐藏

pickerview.hidden=YES;

显示

pickerview.hidden=NO;
于 2011-05-26T10:06:48.410 回答
0

尝试更改frameaspickerView

pickerView.frame = CGRectMake(10,10,100,100);
于 2011-05-26T10:08:29.427 回答
0

在你的代码subView.frame=CGRectMake(180, 120, 150, 150);pickerView.frame=CGRectMake(190, 130, 100, 100);

选择器视图是您的“子视图”的子视图

这里pickerview的框架开始超出子视图的边界,即原点x是190。但子视图的宽度只有150。

所以正确的代码是

pickerView.frame=CGRectMake(0, 0, 100, 100);

因为pickerview 是自定义视图的子视图。它应该在其父视图的框架中。

于 2011-05-26T10:19:06.927 回答