Search code examples
iphoneiosuipickerview

UIPickerView select and hide


How do you make a UIPickerView act like the one with a webview wherein there is a drop down selection box and instead of dropping down like usual websites do, the iphone makes it into a UIPickerView with all the selections in. When you select one, a check becomes visible beside your selection and changes the value of the drop box. And how do you put the "Done" button on top of the UIPickerView to dismiss the UIPickerView?

I already know that [pickerview setHidden:YES] is the method to use to hide the pickerview. I just don't know how to include the "Done" button in the UIPickerView.

Regards, Chris


Solution

  • The "Done" button is placed in UIToolBar.

    Use the below method of UIToolBar for adding the "Done" buttons.

    - (void)setItems:(NSArray *)items animated:(BOOL)animated {
    
        UIToolbar*  mypickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 56)];
        mypickerToolbar.barStyle = UIBarStyleBlackOpaque;
        [mypickerToolbar sizeToFit];
    
        NSMutableArray *barItems = [[NSMutableArray alloc] init];
    
        UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
        [barItems addObject:flexSpace];
    
        UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(DatePickerDoneClick)];
        [barItems addObject:doneBtn];
    
        [mypickerToolbar setItems:barItems animated:YES];
    
    }