Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am new to iOs and trying to add the picker UI element dynamically to the UI. In the class - UIPickerView , i don't see any methods for adding the picker to the view. Has anyone tried doing this? Thanks a lot.

share|improve this question
1  
Do you mean you wanna change content of pickerview Dynamically ? – Dharmbir Choudhary Apr 23 at 5:34
what about [self.view addSubview:picker]; ? There are several ways to integrate Picker to view. – Midhun MP Apr 23 at 5:36

4 Answers

i don't see any methods for adding the picker to the view

A UIPickerView is a subclass of UIView, so you use the same methods that you'd use to add any subview to a view, namely -addSubview:, like this:

UIPickerView *picker = [[UIPickerView alloc] initWithFrame:theFrame];
picker.delegate = self;
picker.dataSource = self;
[self.view addSubview:picker];

I'm assuming here that the code is in a view controller that's acting as both the delegate and data source for the picker.

share|improve this answer

First alloc memory to your picker view.

UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 265, 320, 216)];

and then set delegate and datasource.

pickerView.dataSource=self;
pickerView.delegate=self;

after that add picker view to your main view.

[self.view addSubview:pickerView];

after that you should implement pickerview's method .

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

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

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
        return [array objectAtIndex:row];
}

i hope this will work fine.

share|improve this answer

You adding the picker to the view just like and UIView you want to add -> addSubview method.

UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,200,320,200,)]];
pickerView.delegate = self;
[self.view addSubview:pickerView];

Don't forget to set it's delegate and implement it in order to achieve it's functionality.

share|improve this answer

As with other you need to alloc+initWithFrame.

UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 100, 0, 0)];

Then add it to your target view.

[yourView addSubview:pickerView];

Also you need to set it delegate and datasource, which will show all your elements in the picker view.

pickerView.dataSource=self;
pickerView.delegate=self;

And all other customization as :

pickerView.showsSelectionIndicator=YES;

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
  return [array objectAtIndex:row];
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.