2012-12-24 2 views
4

텍스트 필드에 datepicker를 연결하는 데 문제가 있습니다. 내가 stackoverflow에서 모든 솔루션을 시도했지만 그들은 이오 6에서 작동하지 않습니다. 당신이 그것을 만드는 방법을 desribe 수있는, 나는 텍스트 필드를 누르고 datepicker를 통해 날짜를 선택하고 싶습니다.ios 6 : UITextField의 UIDatePicker

+0

나는 많은 시간 전에이 작업을했지만, 결과를 공유하지 않았 으면 여기에서 찾을 수 있습니다. http://ag-up.com/?p=1054 – alterpub

답변

-2
#import "TextfieldwithDatepickerViewController.h" 

UIActionSheet *pickerViewPopup; 

@implementation TextfieldwithDatepickerViewController 
- (void)textFieldDidBeginEditing:(UITextField *)aTextField{ 
    [aTextField resignFirstResponder]; 

    pickerViewPopup = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil]; 

    UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, 0, 0)]; 
    pickerView.datePickerMode = UIDatePickerModeDate; 
    pickerView.hidden = NO; 
    pickerView.date = [NSDate date]; 

    UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; 
    pickerToolbar.barStyle = UIBarStyleBlackOpaque; 
    [pickerToolbar sizeToFit]; 

    NSMutableArray *barItems = [[NSMutableArray alloc] init]; 

    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]; 
    [barItems addObject:flexSpace]; 

    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonPressed:)]; 
    [barItems addObject:doneBtn]; 

    UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonPressed:)]; 
    [barItems addObject:cancelBtn]; 

    [pickerToolbar setItems:barItems animated:YES]; 

    [pickerViewPopup addSubview:pickerToolbar]; 
    [pickerViewPopup addSubview:pickerView]; 
    [pickerViewPopup showInView:self.view]; 
    [pickerViewPopup setBounds:CGRectMake(0,0,320, 464)]; 
} 

-(void)doneButtonPressed:(id)sender{ 
    //Do something here here with the value selected using [pickerView date] to get that value 

    [pickerViewPopup dismissWithClickedButtonIndex:1 animated:YES]; 
} 

-(void)cancelButtonPressed:(id)sender{ 
    [pickerViewPopup dismissWithClickedButtonIndex:1 animated:YES]; 
} 
+0

만들었습니다. 컴파일하고 시작했지만 pickview 또는 picktoolbar가 표시되지 않았습니다. 그냥 스탠드 아트 키보드가 나타났습니다. 텍스트 필드에 datepicker가있는 프로젝트 소스를 줄 수 있습니까? 아마도 그것을 github에 넣을 수 있을까요? – alterpub

2

당신은 키보드 대신 피커 쇼를 만들기 위해 UITextFieldinputView 속성을 사용합니다. 그러면 iOS6이 당신을 위해 모든 것을 처리 할 것입니다 (키보드 대신에 애니메이션으로 선택기를 보여주는 등).

참고 : 당신은 아마 사용자가 선택기 (것이다 단순히 "OK"버튼을 IBAction을 기각 할 수 있도록 (거기에 약간의 "OK"버튼을 일반적으로 UIToolBar을) 너무 일부 inputAccessoryView를 추가 할 것 에는 입력을 확인하는 버튼이 없습니다 (키보드는 "Return Key"가 있습니다).