2013-07-21 57 views

回答

4

我最近只需要做同樣的事情。

  1. 插入UITextField到您的UITableViewCell(您可能需要創建一個自定義UITableViewCell取決於你是否希望它出現在您的UITableView的每一個動態的細胞或單細胞靜態)。
  2. UIDatePicker創建屬性,一個UIToolbarUITextField,然後在IB掛鉤的UITextField屬性在第1步(創建了UITextField如果您正在使用自定義UITableViewCell類,這也正是UITextField財產需要去):

    @property (strong, nonatomic) UIDatePicker * datePicker; 
    @property (strong, nonatomic) UIToolbar * datePickerToolbar; 
    @property (strong, nonatomic) IBOutlet UITextField *textField; 
    
    ... 
    
    @synthesize datePicker, datePickerToolbar, textField; 
    
  3. 設置你UIDatePickerUIToolbarUITextField

    - (void)viewDidLoad { 
        // Initialise UIDatePicker 
        datePicker = [[UIDatePicker alloc] init]; 
        datePicker.datePickerMode = UIDatePickerModeTime; 
        [datePicker addTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged]; // method to respond to changes in the picker value 
    
        // Setup UIToolbar for UIDatePicker 
        datePickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)]; 
        [datePickerToolbar setBarStyle:UIBarStyleBlackTranslucent];  
        UIBarButtonItem *extraSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 
        UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(dismissPicker:)]; // method to dismiss the picker when the "Done" button is pressed  
        [datePickerToolbar setItems:[[NSArray alloc] initWithObjects: extraSpace, doneButton, nil]]; 
    
    
        // Note: If you're using dynamic cells, the below 2 lines need to be in your cellForRowAtIndexPath method instead. 
    
        // Set UITextfield's inputView as UIDatePicker  
        textField.inputView = datePicker; 
        // Set UITextfield's inputAccessoryView as UIToolbar 
        textField.inputAccessoryView = datePickerToolbar; 
    } 
    
  4. 設置dismissPicker方法:

    -(void)dismissPicker:(id)sender{ 
        [textField resignFirstResponder]; 
    } 
    
  5. 設置datePickerValueChanged方法:

    - (void)datePickerValueChanged:(id)sender{ 
        NSDate *selectedDate = datePicker.date; 
    
        NSDateFormatter *df = [[NSDateFormatter alloc] init]; 
        [df setDateFormat:@"HH:mm"]; 
    
        [textField setText:[df stringFromDate:selectedDate]]; 
    } 
    

注:在我的代碼,我需要的接口來顯示時間,因此日期格式在這裏設置(HH:mm)。因此,您還會注意到,在viewDidLoadUIDatePicker初始化代碼中,我已將其模式設置爲UIDatePickerModeTime。對於日期選擇,您可能需要將其設置爲UIDatePickerModeDate

+0

這是偉大的,但是,你可以詳細說明一些事情。首先,當我嘗試退出datepicker時出現此錯誤: - [UIBarButtonItem resignFirstResponder]:無法識別的選擇器,並且可以解釋如何將textfields文本設置爲用戶設置的日期。會不會像textField.text = ?? – Apollo

+0

@Auser哎呀,請檢查我的'dismissPicker'代碼上的編輯。另外,我添加了一個新方法的代碼,以響應'UIControlEventValueChanged'以及在'UIDatePicker'上設置的代碼。 – Yazid

+0

這真是太棒了。感謝您抽出寶貴時間來幫助我解決問題。真的很感激它。 – Apollo

3

您可以在UITableViewCell中放入一個不可見的UITextField,並將UITextFields的inputview設置爲UIDatePicker。在這裏我們的代碼:

yourdatePicker = [[UIDatePicker alloc]init]; 
yourtextField.inputView = yourdatePicker; 
0

我正在使用ActionSheetPicker [1]。它允許您從部分重疊當前場景的操作表中顯示任何類型的選取器。此外,您可以添加按鈕(如取消或「今天」)和視圖頂部的標題。

[1]:原始版本由Tim Cinel:https://github.com/TimCinel/ActionSheetPicker 通過改進哈日卡勒姆·辛格:在我的叉子https://github.com/Club15CC/ActionSheetPicker 固定棄用(我認爲現在蔓延到其他人的):https://github.com/booiiing/ActionSheetPicker