2012-08-29 2 views
0

버튼을 클릭하면 날짜 선택 도구를 성공적으로 표시 할 수 있습니다. 그러나 날짜 선택 도구에서 날짜를 선택하면 UIbutton에 선택한 날짜를 표시 할 수 없습니다.datepicker를 사용하여 버튼에서 선택한 날짜를 표시하는 방법은 무엇입니까?

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

답변

1

당신은 당신이 보여주고있다 날짜의 this.Create 배열에 대한 UIPickerViewDataSource,UIPickerViewDelegate 방법을 사용해야합니다 pickerView 다음 메소드를 사용하십시오.

- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component 
{ 

[ closeButton setTitle:[NSString StringWithFormat:@"%@",[datesArray objectAtIndex:row]] forState:UIControlStateNormal]; 
} 
+0

날짜 배열이 아닌 datepicker에 캘린더를 표시하고 싶습니다. –

+0

다음은 http : // stackoverflow와 정확히 일치하는 링크입니다. com/questions/4841205/xcode-saving-uidatepicker-to-a-label를 즐기십시오! – SriKanth

1

누구의 클릭 이벤트 pickerview 날짜 선택에서 shown.on 선택 날짜가 선택한 날짜

에 레이블 텍스트를 업데이트됩니다의 버튼에 하위 뷰 일부 레이블을 추가 PLZ 여기

내 코드 나에게 도움이
+0

니스는 여기^_^ –

0

사용이 버튼에 제목을 업데이트

[ closeButton setTitle:str forState:UIControlStateNormal]; 
+0

을 당신을보고 :) 완료 str은 무엇입니까 ?? plz str에 대한 코드를 제공합니다 –

+0

그것은 나보다 많이 일했습니다. –

+0

도움이된다면 대답을 받아들입니다. –

0

함께 IBOutlet과를 사용하여 버튼의 연결을 수행 한 후 다음과 같은 방법으로

- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component 
    { 
     [ closeButton setTitle:[NSString StringWithFormat:@"%@",[datesArray objectAtIndex:row]] forState:UIControlStateNormal]; 

    } 

를 사용하여 버튼의 텍스트를 설정 그리고 당신은

관련 문제