views:

374

answers:

2

I'm getting crashes from my UIDatePicker view and I think it's because I'm not retaining the pickers selected date. Can anyone tell me if this could be correct?

I have a modal view for selecting a toDate and a fromDate range. These values are passed into the modal view and grabbed out of the view when it's dismissed. The view has one UIDatePicker and a segmented button for switching between the to and from dates.

Every time the segmented control switches I set the pickers date to the matching to or from date. When the picker value changes I update the to or from dates accordingly. The view crashes after a couple of switches between these dates.

I'm not retaining the pickers selected date so I'm guessing when I set the value of the pickers date from the toDate to the fromDate the toDate is being released so when I switch the picker back to the toDate it's going to crash.

Also to use the selected date from the picker outside the view will the date need to be retained as the picker will be released along with the date?

Does this make sense to anyone?

+1  A: 

If you need to grab the date value from a UIDatePicker, you indeed need to retain a copy if you'll be using it outside the scope of the function (say, over multiple AutoreleasePool cycles).

Getting the date from a UIDatePicker will retain a reference but it will be autoreleased so effectively is only valid until the autorelease pool is destroyed.

Remember to release your reference once you've finished with it.

For simply using it temporarily inside a function, you won't need to retain it as stated above.

Nick Bedford
A: 

Why don't you set a property of your viewvcontroller or other class to the date you get using:

self.date = date;

Define date to be a @property with a retain attribute. That way you should be able to use date in other places of your app, and it won't be autoreleased when you go through the runloop.

mahboudz
If I do this and then say self.date = [picker date]; then wouldn't self.date point to the pikers date and be released when the pickers date is set to another date. ie [picker setDate:date2];
McKay