代表不能正常工作

时间:2010-04-21 13:18:29

标签: iphone date delegates

我是iPhone开发的新手。我正在将日期转换为所需的格式并将其设置为委托并在另一个视图中获取其值。当我尝试从委托中获取值时会话重新启动。如果我在设置委托中设置原始日期而不是格式化日期,那么我可以在另一个视图中获取该值。如果我还提供任何静态字符串值,那么我也能够返回静态字符串值。仅设置字符串的格式化日期,然后会话重新启动。如果我打印并检查格式化日期的值,它只打印正确的格式化日期。请帮助我。这是我的日期转换代码

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];

编辑: 这将显示在控制台

内部视图确实加载了

[会议开始于2010-04-21 19:12:53 +0530。] GNU gdb 6.3.50-20050815(Apple版gdb-967)(2009年7月14日星期二02:11:58) 版权所有2004 Free Software Foundation,Inc。 GDB是免费软件,由GNU通用公共许可证涵盖,您就是 欢迎在某些条件下更改和/或分发它的副本。 输入“show copying”查看条件。 GDB完全没有保修。输入“show warranty”了解详情。 此GDB配置为“i386-apple-darwin”.sharedlibrary apply-load-rules all 附加到过程4216。 (gdb)

在另一个视图中

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

get不能正常工作。如果我提供任何静态字符串或“dateval”,它可以正常工作。

感谢。

1 个答案:

答案 0 :(得分:2)

似乎没有保留

outputDate,因此在事件循环结束时该值会丢失(因为NSAutoreleasePool)。

您应该保留outputDate以避免在委托中发布类似的内容:

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

最好的解决方案是在委托中使用retain属性声明属性。