Ios – UIPickerView with done button not working

iosobjective ctoolbaruibarbuttonitemuipickerview

Picker view with toolbar, created Done button on it. On click Done button its not working.

The picker view scrolling upwards. on click Done button.

-(void)createPicker:(id)sender{

    pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,100,0,0)];
    [pickerView setDataSource: self];
    [pickerView setDelegate: self];
    pickerView.showsSelectionIndicator = YES;
    [pickerView setBackgroundColor:[UIColor whiteColor]];

    UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
    toolBar.barStyle = UIBarStyleBlackOpaque;
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneTouched:)];
    [toolBar setItems:[NSArray arrayWithObjects:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], doneButton, nil]];
    [pickerView addSubview:toolBar];
}

On click done button dismiss the pickerView

-(void)doneTouched:(id)sender{
     [pickerview removeFromSuperview];
 }

I have no clue what i did wrong here. Can any one advice me how to invoke done button method which added on uipickerview toolbar button.

On clicking done picker view is scrolling up, not invoking the method doneTouched:

@All
Thanks In Advance.

Best Answer

I have resolved the issue which do not know is proper way to implement or not, but it work for me. Below code for picker view with done button

-(void)createPickerView{

    pickerToolBarView = [[UIView alloc]initWithFrame:CGRectMake(0,self.view.frame.size.height/2, self.view.frame.size.width,400)];
    [pickerToolBarView setBackgroundColor:[UIColor whiteColor]];

    UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,pickerToolBarView.frame.size.width,42)];
    toolBar.barStyle = UIBarStyleBlackOpaque;
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneTouched:)];
    [toolBar setItems:[NSArray arrayWithObjects:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], doneButton, nil]];

    pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,toolBar.frame.size.height,toolBar.frame.size.width,100)];
    [pickerView setDataSource: self];
    [pickerView setDelegate: self];
    pickerView.showsSelectionIndicator = YES;
    [pickerView setBackgroundColor:[UIColor whiteColor]];

    [pickerToolBarView addSubview:toolBar];
    [pickerToolBarView addSubview:pickerView];
    [self.view addSubview:pickerToolBarView];
    [self.view bringSubviewToFront:pickerToolBarView];
    [pickerToolBarView setHidden:YES];   
}

/* Done Touched */
- (void)doneTouched:(UIBarButtonItem *)sender{
    // hide the  view
    NSLog(@"Done Touched");
    [pickerToolBarView setHidden:YES];
}
Related Topic