Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to give this picker view a datasource

Here I am adding a pickerview programaticly

 - (void)viewDidLoad {
        [super viewDidLoad];
        CGRect pickerFrame = CGRectMake(0,280,321,200);

        UIPickerView *myPickerView = [[UIPickerView alloc] init]; //Not sure if this is the proper allocation/initialization procedure

        //Set up the picker view as you need it

        //Set up the display frame
        myPickerView.frame = pickerFrame; //I recommend using IB just to get the proper width/height dimensions

        //Add the picker to the view
        [self.view addSubview:myPickerView];
    }

But now I need to actually have it display content and somehow find out when it changes and what value it has changed to. How do I do this?

like image 202
Michael Avatar asked Mar 21 '11 20:03

Michael


People also ask

How do you show a picker?

Showing the time picker Once you've defined a DialogFragment like the one shown above, you can display the time picker by creating an instance of the DialogFragment and calling show() . This method calls show() on a new instance of the DialogFragment defined above.

What is picker view in Swift?

A picker view displays one or more wheels that the user manipulates to select items. Each wheel — known as a component — has a series of indexed rows representing the selectable items. Each row displays a string or view so that the user can identify the item on that row.

How do I create a picker view in Swift?

Click the Assistant Editor button and make sure that the storyboard is in the left pane and that ViewController. swift is in the right. Then hold down control and click the UIPickerView element in the storyboard and drag your mouse over to the right side.

What is the purpose of the delegate of a Uipicker view object?

Typically the delegate implements other optional methods to respond to new selections or deselections of component rows. See UIPickerView for a discussion of components, rows, row content, and row selection.


1 Answers

in .h file place this code

@interface RootVC : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>

assign the datasource and delegate to the picker

// this view controller is the data source and delegate
myPickerView.delegate = self;
myPickerView.dataSource = self;

use the following delegate and datasouce methods

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{

}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    return 200;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 50;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSString *returnStr = @"";
    if (pickerView == myPickerView)
    {       
        returnStr = [[levelPickerViewArray objectAtIndex:row] objectForKey:@"nodeContent"];
    }

    return returnStr;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (pickerView == myPickerView)
    {
        return [levelPickerViewArray count];
    }
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}
like image 57
pradeepa Avatar answered Sep 18 '22 14:09

pradeepa