2010-09-02 225 views

回答

8

你應該創建一個UIView來保存選擇器和你需要的按鈕,然後當你需要輸入日期時將該視圖動畫到可見屏幕中,當點擊該按鈕時將其動畫化。我已經使用了這樣的方案,其中按鈕實際上是日期標籤,並且點擊開始或結束日期編輯。

+0

thnx。我一直在想,只是想確保 – humblePilgrim 2010-09-02 08:09:32

+0

Thanx我得到它的工作 – humblePilgrim 2010-09-04 04:21:31

8

我張貼的代碼,請聲明是不宣而按它們的類型休息,你會完成它。希望這會有所幫助的對象...

#pragma mark DatePickerView 
UIImagePickerController* imagePickerController; 
UIDatePicker *theDatePicker; 
UIToolbar* pickerToolbar; 
UIActionSheet* pickerViewDate; 
-(void)DatePickerView 
{ 
    pickerViewDate = [[UIActionSheet alloc] initWithTitle:@"How many?" 
               delegate:self 
             cancelButtonTitle:nil 
            destructiveButtonTitle:nil 
             otherButtonTitles:nil]; 

    theDatePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)]; 
    theDatePicker.datePickerMode = UIDatePickerModeDate; 

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4]; 
    [dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]autorelease]]; 
    [dateFormatter setDateFormat:@"dd MMM yyyy"]; 
    //[dateFormatter setDateFormat:@"MM/dd/YYYY"]; 

    //[theDatePicker release]; 
    [theDatePicker addTarget:self action:@selector(dateChanged) forControlEvents:UIControlEventValueChanged]; 


    pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; 
    pickerToolbar.barStyle=UIBarStyleBlackOpaque; 
    [pickerToolbar sizeToFit]; 
    NSMutableArray *barItems = [[NSMutableArray alloc] init]; 
    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(DatePickerDoneClick)]; 
    [barItems addObject:flexSpace]; 

    [pickerToolbar setItems:barItems animated:YES];  
    [pickerViewDate addSubview:pickerToolbar]; 
    [pickerViewDate addSubview:theDatePicker]; 
    [pickerViewDate showInView:self.view]; 
    [pickerViewDate setBounds:CGRectMake(0,0,320, 464)]; 
} 

-(IBAction)dateChanged{ 

    NSDateFormatter *FormatDate = [[NSDateFormatter alloc] init]; 
    [FormatDate setLocale: [[[NSLocale alloc] 
           initWithLocaleIdentifier:@"en_US"] autorelease]]; 
    [FormatDate setDateFormat:@"MM/dd/YYYY"]; 
    SelectedTextField.text = [FormatDate stringFromDate:[theDatePicker date]]; 
} 


-(BOOL)closeDatePicker:(id)sender{ 
    [pickerViewDate dismissWithClickedButtonIndex:0 animated:YES]; 
    [pickerToolbar release]; 
    [pickerViewDate release]; 
    [SelectedTextField resignFirstResponder]; 


    return YES; 
} 

-(IBAction)DatePickerDoneClick{ 
    [self closeDatePicker:self]; 
    tableview.frame=CGRectMake(0, 44, 320, 416); 

} 

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ 
    return 1; 
} 

// returns the number of rows 
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ 
    return 30; 
    // return [pickerViewArray count]; 
} 

做的修改按要求。這是100%運行的代碼在類似的應用幾乎使用我

編碼快樂......