When user press button show datepicker view and select date and then display as button title and further close datepicker view

谁都会走 提交于 2019-12-04 21:36:50
Noor

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

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

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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!