开发者

how to add uipickerview as as subview to mainview in iphone?

I have one application in which when user clicks a button i want to open uipickerview as as subview to main view and after user selects an item it should be removed from main view.(drop down kind of functionality).For that i have written code as below:

-(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 *)pi开发者_StackOverflowckerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
    return [arraypref objectAtIndex:row];
}

but is only showing subview in the main view not the pickerview.how can i do that any tutorial or source code for that?


Better you use

to hide

pickerview.hidden=YES;

to show

pickerview.hidden=NO;


Try changing the frame of the pickerView as

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


in your code subView.frame=CGRectMake(180, 120, 150, 150); and pickerView.frame=CGRectMake(190, 130, 100, 100);

picker view is a subview of your 'subView'

here pickerview's frame starts out of bounds of subview ie origin x is 190. but the width of the subview is only 150.

so the correct code is

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

since pickerview is a subview of a custom view. it should be in the frame of its parent view.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜