问题
I wrote this code. but not worlking...
Yes NSLog is saw proper date.
Datepicker also saw and select date. But when i select date nothing to changes on my button title and datepicker remain on the screen. I want hide also when user selected date...
Actually i display datepicker view when user press button and then select date and set title to button and release picker view. I use ios 5...
[datePicker setHidden:NO];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"MM/dd/yyyy"];
NSString *dateString = [dateFormatter stringFromDate:[self.datePicker date]];
NSLog(@"\n"
"theDate: |%@| \n"
, dateString);
[self.DOBbutton setTitle:dateString forState:UIControlStateNormal];
回答1:
in your button action write this code
UIActionSheet *dateActionSheet = [[UIActionSheet alloc] initWithTitle:@"" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"Done", nil];
[dateActionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
[dateActionSheet showInView:self.view];
[dateActionSheet setFrame:CGRectMake(0, 200, 320, 383)];
UIDatePicker *dp = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 80, 100, 116)];
dp.datePickerMode = UIDatePickerModeTime;
// NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
// [dateFormat setDateFormat:@"HH:mm:ss"];
NSDate *eventDate = dp.date;
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
[timeFormat setDateFormat:@"HH:mm"];
timeString = [timeFormat stringFromDate:eventDate];
[ dp addTarget: self action:
@selector(dateChanged:)
forControlEvents:UIControlEventValueChanged
];
[ self.view addSubview: dp ];
// txtProposeTime.font = [ UIFont fontWithName: @"Arial" size: 9.0 ];
// txtProposeTime.textColor = [ UIColor blackColor ];
txtProposeTime.text = timeString;
[dp setDatePickerMode:UIDatePickerModeTime];
[dateActionSheet addSubview:dp];
Outside button write this code
-(void) dateChanged: (id)sender
{
UIDatePicker *control = (UIDatePicker *) sender;
control.datePickerMode=UIDatePickerModeDate;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
// [dateFormat setDateFormat:@"MM/dd/yyyy HH:mm:ss a"];
NSDate *eventDate = control.date;
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
[timeFormat setDateFormat:@"dd/MM/yyyy"];
NSString *dateString = [timeFormat stringFromDate:eventDate];
txtMeetingDate.text=dateString;
}
回答2:
try this
replace it
[self.DOBbutton setTitle:dateString forState:UIControlStateNormal];
with
DOBbutton.titleLabel.text =dateString;
and make sure button is connected with your xib or not
回答3:
Set button with title Done and when user select Done at that time called bellow method...
-(IBAction)btnDone_Clicked:(id)sender
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
NSString *dateString =[[yourDatePicker date] stringFromDateWithFormat:@"MM/dd/yyyy"];
[yourButton setTitle:dateString forState:UIControlStateNormal];
[yourDatePicker setHidden:YES];
[UIView commitAnimations];
}
and this is my custome method which useful for change the format of Date and convert it to string..
-(NSString*)stringFromDateWithFormat:(NSString*)strDateFormat{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//[formatter setTimeZone:[NSTimeZone systemTimeZone]];
[formatter setDateFormat:strDateFormat];
NSString *str = [formatter stringFromDate:self];
[formatter release];
return str;
}
来源:https://stackoverflow.com/questions/16030559/when-user-press-button-show-datepicker-view-and-select-date-and-then-display-as