2015-02-04 8 views
0

사람들 현재 날짜를 선택할 수없는 datepicker를 어떻게 구현했는지 알 수 없습니다. 나 또한 날짜 피커가 튀어 오를 때 완료된 버튼을 보여주기 위해 많은 답을 따랐다.UiDatePicker Xcode 6에서 레이블에 현재 날짜가 표시되지 않고 완료 또는 취소 버튼이 표시되지 않습니다.

기본적으로 완료 버튼과 현재 날짜의 라벨 설정에 도움이됩니다. 여기

다음 코드를 대신 UILabel의의의 UITextField를 이용할 수 있도록 스크린 샷 enter image description here

- (IBAction)btnDate:(id)sender { 
datepicker=[[UIDatePicker alloc] initWithFrame:CGRectMake(0, 250, 325, 300)]; 
datepicker.datePickerMode = UIDatePickerModeDate; 
datepicker.hidden = NO; 
datepicker.date = [NSDate date]; 

[datepicker addTarget:self 
       action:@selector(LabelChange:) 
    forControlEvents:UIControlEventValueChanged]; 
datepicker.backgroundColor = [UIColor whiteColor]; 
[self.view addSubview:datepicker]; //this can set value of selected date to your label change according to your condition 


NSDateFormatter * df = [[NSDateFormatter alloc] init]; 
[df setDateFormat:@"M-d-yyyy"]; // from here u can change format.. 
lblDate.text=[df stringFromDate:datepicker.date]; 

}

- (void)LabelChange:(id)sender{ 
    NSDateFormatter *df = [[NSDateFormatter alloc] init]; 
    [df setDateFormat:@"M-d-yyyy"]; 
    lblDate.text = [NSString stringWithFormat:@"%@", 
          [df stringFromDate:datepicker.date]]; 
    [datepicker removeFromSuperview]; 

} 
+0

시도 [날짜 선택기의 addsubview가 : BTN] – SRI

+0

내가 시도하고 심지어 복사 및이 블로그 튜토리얼에서이 코드를 붙여 [링크]에 http : // mkonstantinou .blogspot.com/2012/12/adding-done-button-to-uidatepicker.html하지만 아직 표시되지 않고 현재 날짜를 선택할 수 없습니다. –

+0

코드를 게시하시기 바랍니다. – SRI

답변

0

이 유용한 자습서로 저의 질문에 대한 답을 찾았습니다. 다음은 코드 선택기 아래에있는 링크에서 가져온 취소 버튼을 사용하여 날짜 선택기를 사용하여 작성자에게 자세한 정보와 크레딧을 제공하려는 사용자가 사용할 수있는 코드입니다. 같은 날짜 선택기에 하위 뷰로있는 UIButton을 추가 할 수

/* 
* Change the date. 
*/ 
- (void)changePickUpDate:(UIDatePicker *)sender { 
    NSString *formatString = [NSDateFormatter dateFormatFromTemplate:@"yyyyMMdd" options:0 locale:[NSLocale currentLocale]]; 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:formatString]; 

    NSString *todayString = [dateFormatter stringFromDate:sender.date]; 
    NSLog(@"todayString: %@", todayString); 

    DateLabel.text =[[NSString stringWithFormat:@"%@", todayString]init]; 
    //NSLog(@"New Date: %@", sender.date); 
} 

/* 
* Releases the background, datepicker and toolbar. 
*/ 
- (void)removeViews:(id)object { 

    /* 
    * Releases the background view. 
    */ 
    [[self.view viewWithTag:9] removeFromSuperview]; 
    /* 
    * Releases the datepicker. 
    */ 
    [[self.view viewWithTag:10] removeFromSuperview]; 
    /* 
    * Releases the toolbar. 
    */ 
    [[self.view viewWithTag:11] removeFromSuperview]; 
} 

/* 
* Hides the datapicker. 
*/ 
- (void)dismissDatePicker:(id)sender { 

    /* 
    * Create a variable with the target position and size for hiding the toolbar. 
    */ 
    CGRect toolbarTargetFrame = CGRectMake(0, self.view.bounds.size.height, 320, 44); 
    /* 
    * Create a variable with the target position and size for hiding the datepicker. 
    */ 
    CGRect datePickerTargetFrame = CGRectMake(0, self.view.bounds.size.height + 44, 320, 216); 

    /* 
    * Send start animation message to UIView. 
    */ 
    [UIView beginAnimations:@"MoveOut" context:nil]; 
    /* 
    * Set the transparency for hiding the background view. 
    */ 
    [self.view viewWithTag:9].alpha = 0; 
    /* 
    * Set the target position for hiding the datepicker. 
    */ 
    [self.view viewWithTag:10].frame = datePickerTargetFrame; 
    /* 
    * Set the target position for hiding the toolbar. 
    */ 
    [self.view viewWithTag:11].frame = toolbarTargetFrame; 
    /* 
    * The method setAnimationDelegate enables knowledge on start and end of the animation. 
    */ 
    [UIView setAnimationDelegate:self]; 
    /* 
    * Calls the removeViews method at the end of the animation. 
    */ 
    [UIView setAnimationDidStopSelector:@selector(removeViews:)]; 
    /* 
    * Commits the animation thread for execution. 
    */ 
    [UIView commitAnimations]; 
} 

/* 
* Sets up and shows the datepicker. 
*/ 
- (IBAction)callPickUpDP { 
    /* 
    * If view with tag exists ignore the rest of the code and exit. 
    */ 
    if([self.view viewWithTag:9]) { 
     return; 
    } 

    /* 
    * Create a variable with the target position and size for showing the toolbar. 
    */ 
    CGRect toolbarTargetFrame = CGRectMake(0, self.view.bounds.size.height - 216 - 44, 320, 44); 
    /* 
    * Create a variable with the target position and size for showing the datepicker. 
    */ 
    CGRect datePickerTargetFrame = CGRectMake(0, self.view.bounds.size.height - 216, 320, 216); 

    /* 
    * Instantiate a UIView with the frame size of the existing view. 
    */ 
    UIView *darkView = [[UIView alloc] initWithFrame:self.view.bounds]; 
    /* 
    * Set the transparency. 
    */ 
    darkView.alpha = 0; 
    /* 
    * Set the background color. 
    */ 
    darkView.backgroundColor = [UIColor blackColor]; 
    /* 
    * Set a tag for the UIView instance to reference it by tag. 
    */ 
    darkView.tag = 9; 
    /* 
    * Setup a tap gesture listener that calls the dismissDatePicker method on tap. 
    */ 
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissDatePicker:)]; 
    /* 
    * Add the tap gesture listener to the UIView. 
    */ 
    [darkView addGestureRecognizer:tapGesture]; 
    /* 
    * Adds the subview on top of all the other subviews. 
    */ 
    [self.view addSubview:darkView]; 

    /* 
    * Instantiate a UIDatePicker and set the initial frame with the datepicker outside of the view. 
    */ 

    UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height+44, 320, 216)]; 
    /* 
    * Set a tag for the UIDatePicker instance to reference it by tag. 
    */ 

    datePicker.alpha = 1; 
    datePicker.tag = 10; 
    datePicker.backgroundColor = [UIColor whiteColor]; 
    datePicker.datePickerMode = UIDatePickerModeDate; 
    /* 
    * Add a listener that listens for changing values in the datepicker which then calls the change date method. 
    */ 
    [datePicker addTarget:self action:@selector(changePickUpDate:) forControlEvents:UIControlEventValueChanged]; 
    /* 
    * Adds the subview on top of all the other subviews. 
    */ 
    [self.view addSubview:datePicker]; 

    /* 
    * Instantiate a UIToolbar and set the initial frame with the toolbar outside of the view. 
    */ 
    UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height, 320, 44)]; 
    /* 
    * Set a tag for the UIToolbar instance to reference it by tag. 
    */ 
    toolBar.tag = 11; 
    /* 
    * Set a style for the UIToolbar. 
    */ 
    toolBar.barStyle = UIBarStyleBlackTranslucent; 
    /* 
    * Instantiate a spacer UIBarButtonItem. 
    */ 
    UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 
    /* 
    * Instantiate a done UIBarButtonItem on click this will call the dismissDatePicker method. 
    */ 
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissDatePicker:)]; 
    /* 
    * Set the created UIBarButtonItems to the toolbar. 
    */ 
    [toolBar setItems:[NSArray arrayWithObjects:spacer, doneButton, nil]]; 
    /* 
    * Adds the subview on top of all the other subviews. 
    */ 
    [self.view addSubview:toolBar]; 

    /* 
    * Start animation. 
    */ 
    [UIView beginAnimations:@"MoveIn" context:nil]; 
    /* 
    * Set the target position for showing the toolbar. 
    */ 
    toolBar.frame = toolbarTargetFrame; 
    /* 
    * Set the target position for showing the datepicker. 
    */ 
    datePicker.frame = datePickerTargetFrame; 
    /* 
    * Set the transparency for showing background view. 
    */ 
    darkView.alpha = 0.5; 
    /* 
    * Commits the animation thread for execution. 
    */ 
    [UIView commitAnimations]; 
} 

iOS – How To Make DatePicker

0

시도이다 : 이런 식으로

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 

    // Create date picker 

    UIDatePicker * datePicker = [UIDatePicker new]; 

    // Create and configure toolbar 

    UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 44)]; 

    UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone 
                       target:self 
                       action:@selector(doneAction:)]; 
    UIBarButtonItem* cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel 
                        target:self 
                        action:@selector(cancelAction:)]; 
    UIBarButtonItem* flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace 
                       target:nil 
                       action:nil]; 
    [toolbar setItems:@[cancelButton, flexibleSpace, doneButton]]; 

    // Set date picker as input view 

    self.textField.inputView = datePicker; 

    // Set toolbar as accessory view 

    self.textField.inputAccessoryView = toolbar; 
} 

- (void)doneAction:(id)sender 
{ 
    UIDatePicker* datePicker = (UIDatePicker*)[self.textField inputView]; 
    self.textField.text = datePicker.date.description; 
    [self.textField resignFirstResponder]; 
} 

- (void)cancelAction:(id)sender 
{ 
    [self.textField resignFirstResponder]; 
} 

, 날짜 선택이 나타납니다 textField를 누르거나 전화 할 때마다 becomeFirstResponder

+0

버튼을 UITextField로 변경하려고했지만 문제가 있습니다.이 충돌이 발생합니다. 한 번만 경고 : 제약 조건이 테이블 뷰 셀의 콘텐츠 뷰에 ​​대해 0의 높이를 모호하게 제안하는 경우를 감지했습니다. 의도하지 않은 붕괴와 대신 표준 높이 사용을 고려 중입니다. –

+0

나를 채팅방에 초대해주십시오. –