0

when i click on my button am able to show date picker sucessfully.But when i am selecting date from the date picker I am unble to show the selected date on UIbutton. plz help me

Here is my code:

-(IBAction)btnDateClicked:(id)sender
{ 
UIActionSheet *actionSheet1 = [[UIActionSheet alloc] initWithTitle:nil 
                                                          delegate:nil
                                                 cancelButtonTitle:nil
                                            destructiveButtonTitle:nil
                                                 otherButtonTitles:nil];

[actionSheet1 setActionSheetStyle:UIActionSheetStyleBlackTranslucent];

CGRect pickerFrame = CGRectMake(0, 40, 0, 0);


UIDatePicker *pView=[[UIDatePicker alloc] initWithFrame:pickerFrame];

pView.datePickerMode=UIDatePickerModeDate; 

NSDateFormatter *df=[[NSDateFormatter alloc] init];

[df setDateFormat:@"MM/dd/yyyy"];

// NSDate *date=[df dateFromString:self.hController.fromDateTime];

// [pView setDate:date animated:YES];

self.datePicker=pView;

[actionSheet1 addSubview:self.datePicker];

[pView release];

UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"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(dismissActionSheet1:) forControlEvents:UIControlEventValueChanged];

[actionSheet1 addSubview:closeButton];

[closeButton release];

[actionSheet1 showInView:[[UIApplication sharedApplication] keyWindow]];

[actionSheet1 setBounds:CGRectMake(0, 0, 320, 485)];

self.actionSheet=actionSheet1;

[actionSheet1 release];
}
///////////////////////////////////
- (void)dismissActionSheet1:(id)sender
{
[self.actionSheet dismissWithClickedButtonIndex:0 animated:YES];

NSDateFormatter *df=[[NSDateFormatter alloc] init];

[df setDateFormat:@"MM/dd/yyyy"];   

//NSString *str=[df stringFromDate:self.datePicker.date]; 

//self.hController.selectDate=str;    
}
SriKanth
  • 459
  • 7
  • 27

4 Answers4

1

Add some label as subview to the button on whose click event pickerview is shown.on selecting date from date picker update the label text to the selected date

Pratyusha Terli
  • 2,343
  • 19
  • 31
0

Use this to update title on button

[ closeButton setTitle:str forState:UIControlStateNormal];
Dushyant Singh
  • 721
  • 4
  • 13
0

You have to use UIPickerViewDataSource,UIPickerViewDelegate methods for this.Create an array of your dates Which you are showing in the pickerView and use the following method.

- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component 
{

[ closeButton setTitle:[NSString StringWithFormat:@"%@",[datesArray objectAtIndex:row]] forState:UIControlStateNormal];
}
SriKanth
  • 459
  • 7
  • 27
  • I want to show the calender in datepicker not an array of dates –  Aug 29 '12 at 12:21
  • Here is an exact link for you http://stackoverflow.com/questions/4841205/xcode-saving-uidatepicker-to-be-displayed-in-a-label enjoy! – SriKanth Aug 29 '12 at 12:26
0

Do connection of button using IBOutlet and and then in following method set the text of button using

  - (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component
     {
        [ closeButton setTitle:[NSString StringWithFormat:@"%@",[datesArray objectAtIndex:row]]  forState:UIControlStateNormal];

     }

And you are done :)

Rohit
  • 181
  • 1
  • 10