views:

725

answers:

2

I have a View Controller I'm reusing to edit individual objects (NSString, NSNumber, NSDate) in a previous View Controller's TableView. I'm loosely basing this off of the EditingViewController in Apple's SQLiteBooks sample project. the datePicker is instantiated in the nib file.

The issue here is it works fine & dandy for the first date I set with it. BUT, when called again on a 2nd date it will correctly display the next date I'm editing in its tumblers(?) when it loads, however, the highlighted (blue) month, day & year are those from when the picker was first displayed BEFORE the first edit. How can I set those highlighted values to the initial value of the 2nd date object or reset the whole UIdatePicker instance.

2nd related question (bonus points ;-) as I struggle to wrap my head around Cocoa: why, in the SQLiteBooks example is there no @property or @synthesize statements for the datePicker declared in EditingVewController.h? Or have I overlooked the obvious? Thanks!

Edit: After some experimentation with other apps using UIDatePickers it appears that for whatever reason UIDatePicker when used in the Date mode will highlight the current month, day & year even if you're editing an date from say 1776. HOW can we turn this behavior off?

A: 

2nd related question (bonus points ;-) as I struggle to wrap my head around Cocoa: why, in the SQLiteBooks example is there no @property or @synthesize statements for the datePicker declared in EditingVewController.h? Or have I overlooked the obvious? Thanks!

You only need to declare an @property if you want to make the property public. For private instance variables, you leave it out.

And @synthesize just has the compiler create accessor methods for your @property, so if it's not an @property, you don't use @synthesize (or @dynamic).

Peter Hosey
+1  A: 

Store the date from the first picker in, e.g. storedDate. Then set the date to be displayed in your UIDatePicker using its date property. Assume your UIDatePicker is named picker, then

 picker.date = storedDate; // picker's date is initialised to today's date/time

You can also use this method if you want the picker to animate to your date:

[picker setDate:storedDate animated:YES];

As for the properties, it's not true that they should only be used on public data. Properties refer to data that is accessed through methods instead of being directly accessed through instance variables. The property declarations (specifically, the @synthesize) tell the compiler to use these declarations to automatically generate accessor methods as needed to satisfy your class's interface.

The dot syntax in Objective-C is translated by the compiler to the invocation of accessor methods. So...

foo.bar = @"hello";

Is translated by the compiler to...

[foo setBar:@"hello"];

setBar, also generated, will do all it should according to its @property declarations. So if it is declared as retain, it will be correctly retained in its setter, without you having to worry about it.

So, in conclusion, while the Apple code you mention may work correctly (I haven't looked), it would be better defensive coding to use properties.

Jane Sales