2012-12-24 55 views
4

我在使用文本字段連接日期選擇器時遇到問題。我已經嘗試了從stackoverflow的所有解決方案,但他們不在ios 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會照顧你的一切(然後用動畫而不是鍵盤顯示選取器等)。

注:你可能會想添加一些inputAccessoryView太(通常爲UIToolBar,在它的一些「OK」按鈕),以使用戶能夠關閉該選擇器(你的「OK」按鈕IBAction會簡單因爲UIDatePicker沒有任何按鈕來驗證你的輸入(而鍵盤有它的「返回鍵」)