Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show uidatepicker from uitableviewcell [closed]

How can I show a UIDatePicker from UITableViewCell, and have the datepicker have a toolbar at the top that lefts you resign it? Are there any good tutorials on how to do this?

like image 411
Apollo Avatar asked Jul 21 '13 19:07

Apollo


2 Answers

You could put an invisible UITextField in the UITableViewCell and set the UITextFields inputview to a UIDatePicker. Here us the code:

 yourdatePicker = [[UIDatePicker alloc]init];
 yourtextField.inputView = yourdatePicker;
like image 43
Abdullah Shafique Avatar answered Oct 17 '22 13:10

Abdullah Shafique


I've recently just had to do the exact same thing.

  1. Insert a UITextField into your UITableViewCell (you may need to create a custom UITableViewCell depending whether you want it to appear on every dynamic cell of your UITableView or on a single static cell).
  2. Create properties for a UIDatePicker, a UIToolbar, and a UITextField, then in IB hook up the UITextField property to your UITextField created in Step 1 (If you're using a custom UITableViewCell class, that's where the UITextField property would need to go):

    @property (strong, nonatomic) UIDatePicker * datePicker;
    @property (strong, nonatomic) UIToolbar * datePickerToolbar;
    @property (strong, nonatomic) IBOutlet UITextField *textField;
    
    ...
    
    @synthesize datePicker, datePickerToolbar, textField;
    
  3. Setup your UIDatePicker, UIToolbar, and UITextField:

    - (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. Setup dismissPicker method:

    -(void)dismissPicker:(id)sender{
        [textField resignFirstResponder];
    }
    
  5. Setup datePickerValueChanged method:

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

Note: In my code, I needed the interface to display the time, hence the date format set here (HH:mm). Because of this, you will also notice that in my UIDatePicker initialization code in viewDidLoad, I've set its mode to UIDatePickerModeTime. For date selections, you may want to set it to UIDatePickerModeDate.

like image 149
Yazid Avatar answered Oct 17 '22 15:10

Yazid