0

i have a uipickerivew in uitablview prototype cell, i want to hide only when done button pressed. please help me.

here is my code for how i create uipickeriview and done button.

 self.pickerView = [[UIPickerView alloc] initWithFrame:(CGRect){{0, 0}, 330, 200}];
self.pickerView.delegate = self;
self.pickerView.dataSource = self;
self.pickerView.center = (CGPoint){160, 640};
self.pickerView.hidden = YES;
self.pickerView.backgroundColor =[UIColor whiteColor];

[self.view addSubview:self.pickerView];





UIToolbar *toolBar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,320,44)];
[toolBar setBarStyle:UIBarStyleBlackOpaque];
UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];

UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                                  style:UIBarButtonItemStyleBordered
                                                                 target:self
                                                                 action:@selector(pickerDoneClicked)];
toolBar.items = @[barButtonDone];
toolBar.items = @[flex, barButtonDone];
barButtonDone.tintColor = [UIColor lightGrayColor];
[_pickerView addSubview:toolBar];


-(void) pickerDoneClicked {

[_pickerView resignFirstResponder];

}

Peter Hornsby
  • 4,208
  • 1
  • 25
  • 44
sandeep tomar
  • 303
  • 1
  • 5
  • 17

5 Answers5

3

It is better to rather than create and remove each time, create your pickerview once in view did load and then in your cell do:

[_pickerView becomeFirstResponder];

(to show it like a keyboard animated in).

You can then create a pickerDoneClicked method (that is called when Done is tapped) to call resignFirstResponder on your pickerview (to animate it away):

-(void) pickerDoneClicked {
[_pickerView resignFirstResponder];
}
Joe Benton
  • 3,703
  • 1
  • 21
  • 17
0

just add below IBAction method and call removefromsuperview method,

-(void)pickerDoneClicked{
[self.pickerView removeFromSuperview];}

As you are already adding again everytime, you can directly remove it from superview and adding again.

Parth Pandya
  • 1,460
  • 3
  • 18
  • 34
0

After selecting a value using picker view you can implement this method.Add a toolbar with done button and give action to done button

- (IBAction)doneClicked:(id)sender {
 [yourTextfield resignFirstResponder];
}

this method is used in the case , when selecting a value to the textfield with picker as input.

trainer
  • 1
  • 1
  • 3
0
- (void)viewDidLoad {

  self.pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 330, 200)];
  self.pickerView.delegate = self;
  self.pickerView.dataSource = self;
  self.pickerView.backgroundColor =[UIColor whiteColor];

 [self.view addSubview:self.pickerView];

   UIToolbar *toolBar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,320,44)];
  [toolBar setBarStyle:UIBarStyleBlackOpaque];
 UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                                 style: UIBarButtonItemStylePlain
                                                                target:self
                                                                action:@selector(pickerDoneClicked:)];
   UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];

        toolBar.items = @[flex, barButtonDone];
       barButtonDone.tintColor = [UIColor lightGrayColor];
     [self.view addSubview:toolBar];


   }


-(IBAction)pickerDoneClicked:(id)sender
  {
         pickerView.hidden = YES;

   }
pradip kikani
  • 627
  • 6
  • 14
-1

Inside the didiSelectRow method add this line of code:

self.view.endEditing(true)