如何使用datepicker在按钮上显示所选日期? [英] How to show selected date on button using datepicker?

查看:90
本文介绍了如何使用datepicker在按钮上显示所选日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我点击我的按钮时,我能够成功地显示日期选择器。但是当我从日期选择器中选择日期时,我无法在 UI按钮上显示所选日期。 PLZ帮帮我

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

这是我的代码:

-(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;    
}


推荐答案

你必须使用 UIPickerViewDataSource,UIPickerViewDelegate 此方法。创建日期数组您在pickerView中显示并使用以下方法。

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];
}

这篇关于如何使用datepicker在按钮上显示所选日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆