开发者

How do i take a date out of a datepicker and then put it back in again?

I've tried numerous ways to do this but i can't seem to get it to work. So here's more precisely what i want - you press a bu开发者_运维技巧tton, it takes the date and time set in the datepicker and stores it (in an NSDate most likely, although i have tried converting it back and forth between that and an NSString - didn't work). Then you press another button and it puts the date and time stored and sets the datepicker as that date and time.

Suggestions on how I could do this?


@property (retain) NSDate *savedDate;
@property (retain) IBOutlet UIDatePicker *datePicker;

// To save the date:
self.savedDate = datePicker.date;

// To put it back into the picker:
datePicker.date = self.savedDate;


You actually process Date picker changes as they are made, register the UIControlEventValue changed with a method on your class via the selector paradigm:

[dateSelector addTarget:self
   action:@selector(processDateSelection:)
   forControlEvents:UIControlEventValueChanged];

Then in that method process the time on the picker in that method (mine writes it bac to a UITextField:

control.text = [NSString stringWithFormat:@"%@", [df stringFromDate: dateSelector.date]];

To set the date in the picker, use an NSDate object (With a dateFormatter):

[df setDateFormat:@"yyyy-MM-dd"];
   NSDate *defaultDate = [df dateFromString: textField.text];
   dateSelector.date = defaultDate;

You will also need at method to dismiss your date picker as there is no logical "end" like in a standard Picker. I just bound a method to button, set it as the UIAccessoryView on the Date picker, and that putton tap dismissed the picker.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜