views:

72

answers:

1

I am new to iPhone development. I am converting the date to the desired format and set it to the delegate and get its value in the another view. The session restarts when I tried to get the value from delegate. If I set the original date and not the formatted date in the set delegate, then i able to get the value in the another view. If I also give any static string value, then also I am able to the static string value back. Only the formatted date which is string is set then the session restarts. If i print and check the value of the formatted date it prints the correct formatted date only.Please help me out.Here is my code for date conversion

NSString *dateval=[[stories objectAtIndex: storyIndex] objectForKey:@"date"];

NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];

[inputFormatter setDateFormat:@"EEE, MMM dd, yyyy"];

NSDate *inputDate = [inputFormatter dateFromString:dateval];

NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];

[outputFormatter setDateFormat:@"MMMM dd"];

NSString *outputDate = [outputFormatter stringFromDate:inputDate];  

AppDelegate *delegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];

[delegate setCurrentDates:outputDate];

EDIT: This is displayed in console

inside view did load

[Session started at 2010-04-21 19:12:53 +0530.] GNU gdb 6.3.50-20050815 (Apple version gdb-967) (Tue Jul 14 02:11:58 UTC 2009) Copyright 2004 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "i386-apple-darwin".sharedlibrary apply-load-rules all Attaching to process 4216. (gdb)

In another view

 - (void)viewDidLoad {
NSLog(@"inside view did load");
AppDelegate *delegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];
NSString *titleValue=[delegate getCurrentDates];
self.navigationItem.title =titleValue ;
}

The get does not work properly.It works fine if i give any static string or the "dateval".

Thanks.

+2  A: 

outputDate does not seems to be retained, so the value is lost at the end of the event loop (because of the NSAutoreleasePool).

You should retain the outputDate to avoid its release with something like that in the delegate:

- (void)setCurrentDates:(NSString *)value {
    [value retain]; // <- Retain new value
    [date release]; // <- Release old value;
    date = value;
}

The best solution would be to have a declared property in the delegate with the retain attribute.

Laurent Etiemble
I have fixed the setter code to have the correct retain/release order.
Laurent Etiemble
I was able to understand from your previous code itself, what you are trying to say. Thanks it works perfectly.
Warrior