Search code examples
iphoneuipickerviewuiactionsheet

iPhone fill values for UIPickerView


I've created a UIPickerView, which displays on button click. The question is how I fill in the rowItems

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Select Location"
                                                             delegate:self
                                                    cancelButtonTitle:@"Cancel"
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:nil];

    [actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
    UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,100,0,0)];
    pickerView.showsSelectionIndicator = YES;
    pickerView.dataSource = self;
    pickerView.delegate = self;
    [actionSheet addSubview:pickerView];
    [pickerView release];
    UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:NSLocalizedString(@"Done", @"")]];
    closeButton.momentary = YES;
    closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
    closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
    closeButton.tintColor = [UIColor blackColor];
    [closeButton addTarget:self action:@selector(dismissActionSheet:) forControlEvents:UIControlEventValueChanged];
    [actionSheet addSubview:closeButton];   
    [closeButton release];
    [actionSheet showInView:self.view];
    [actionSheet setBounds:CGRectMake(0, 0, 320, 485)];

and How do I get value when "done" is clicked.


Solution

  • you've set the delegate of the alert, and the picker already. Now you have to implement them. You don't fill in data for the picker, the picker asks your delegate for the data.

    You need the mandatory functions defined in these three protocols.

    UIPickerViewDelegate
    UIPickerViewDataSource
    UIActionSheetDelegate

    You have to implement -(IBAction)dismissActionSheet:(UIButton *)sender too.

    and How do I get value when "done" is clicked.

    I would store the value to a instance variable every time the picker changes, and once the done button is clicked I would assign it to my data model.


    Edit: If you want to learn something I would get rid of the copy&paste solution and try to implement it on my own. But YMMV.