1

I have a UIPickerView. I want a done button on top of the pickerview and i want to dismiss the picker view on click of the done button. Can u please help me on this?

Pradeep Reddy Kypa
  • 3,992
  • 7
  • 57
  • 75

3 Answers3

6

add action sheet to your view and then add tool bar with done button at top of action sheet and below add your your picker to action sheet in done button click write below method to dismiss action sheet

[actionSheet dismissWithClickedButtonIndex:0 animated:YES];

actionSheet=[[UIActionSheet alloc] initWithTitle:@"" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
    [actionSheet showInView:self.view];
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0,320,40)];
[pickerToolbar sizeToFit];
    pickerToolbar.barStyle = UIBarStyleBlackTranslucent;
NSMutableArray *barItems = [[NSMutableArray alloc] init];

    UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonSystemItemCancel target:self action:@selector(cancel_clicked:)];
    [barItems addObject:cancelBtn];
    [cancelBtn release];
    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    [barItems addObject:flexSpace];
    [flexSpace release];
    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(done_clicked:)];
    [barItems addObject:doneBtn];
    [doneBtn release];
    [pickerToolbar setItems:barItems animated:YES];
    [actionSheet addSubview:pickerToolbar];
    [barItems release];
    [pickerToolbar release];

UIPickerView *picker = [[UIPickerView alloc] init];
picker.frame = CGRectMake(0, 44, 320, 216);
picker.delegate  = self;
        picker.dataSource = self;
        picker.showsSelectionIndicator = YES;
[actionSheet addSubview:picker];
[picker release];

-(void)done_clicked:(id)sender
{
    [actionSheet dismissWithClickedButtonIndex:0 animated:YES];
}
-(void)cancel_clicked:(id)sender
{
    [actionSheet dismissWithClickedButtonIndex:0 animated:YES];
}
Narayana Rao Routhu
  • 6,303
  • 27
  • 42
  • Can u please post the code on how to add the Actionsheet and how to add pickerview and toolbar to that Actionsheet? It would be very helpful if the code for dismissing the picker view is also provided. thanks a lot for ur response... – Pradeep Reddy Kypa Dec 12 '11 at 07:43
  • I am not able to open the above link. Can u please paste the content here? That would be very helpful... – Pradeep Reddy Kypa Dec 12 '11 at 07:54
  • Thanks a lot...Supperbbb..It should work fine...I will be implementing the same in the eveninig. I will let u know if this works fine..THanks again for the timely help.... – Pradeep Reddy Kypa Dec 12 '11 at 08:03
  • Here in the above code mentioned, i would like to clarify a small doubt. PFB the code.. " UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0,480,32)]; " here u r specifying the width of the pcikerToolBar as "480" which i guess is wrong. I would draw the width of the same with 320. Can u please clarify if i am wrong here? – Pradeep Reddy Kypa Dec 12 '11 at 08:04
  • No need. me to from andhrapradesh – Narayana Rao Routhu Dec 12 '11 at 08:06
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/5763/discussion-between-pradeep-reddy-kypa-and-narayana) – Pradeep Reddy Kypa Dec 12 '11 at 08:08
1

You can use the toolbar for the same purpose. Take a look at this link. Here, the same solution is taken into consideration. Hope that may help you. Thanks.

Community
  • 1
  • 1
Yama
  • 2,649
  • 3
  • 31
  • 63
  • Much better than hacking UIActionSheet – Brian Dec 12 '11 at 07:46
  • @Sarah:The link u have provided deals with much complexity than the required.My requirement just deals with a basic pickerview and a done button on the top and dismissing the pickerview on click of that done button. can we have a simpler version of this? – Pradeep Reddy Kypa Dec 12 '11 at 07:53
0

every body took the action sheet and then added the bar button ,but simply added uibutton from the view and take method remove from the super view. show the bellow example , i am tried this code successful. i think this is very useful code with out using action sheet

 -(void)parserDidEndDocument:(NSXMLParser *)parser { NSLog(@"the marray data is %@",marray); // [self showpicker1]; if (marray.count == 0) { UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Error" message:@"NO DATA FOUND" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil]; [alert show]; }

[self showpicker1];
}

-(void)showpicker1 { //view1=[[UIView alloc] initWithFrame:CGRectMake(55, 59, 171, 80)]; //[self.view addSubview:view1]; pv = [[UIPickerView alloc] initWithFrame:CGRectMake(55,79,171,0)]; pv.delegate = self; pv.dataSource = self; pv.showsSelectionIndicator = YES; [self.view addSubview:pv]; done=[UIButton buttonWithType:UIButtonTypeRoundedRect]; [done setFrame:CGRectMake(55, 59, 171, 20)]; [done setTitle:@"DONE" forState:UIControlStateNormal]; [done addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchDown]; [self.view addSubview:done]; } -(void)dismiss { [pv removeFromSuperview]; [done removeFromSuperview]; }
Sumit Bijvani
  • 8,154
  • 17
  • 50
  • 82
sudhakar
  • 1
  • 1