在viewControllers之间重置AppDelegate属性

时间:2011-03-20 23:29:13

标签: iphone objective-c ios

我正在尝试在各种viewControllers之间共享数据,我正在使用appDelegate中声明的属性。我在我的第一个viewController中设置了属性,当我打印出内容时,一切看起来都很好,但是在我调用自定义类方法之后,属性被设置为一些随机值,每次运行应用程序时都会改变。请参阅以下代码:

appDelegate .h

NSDate *lastUpdated;

@property (nonatomic, retain) NSDate *lastUpdated;

viewController .m

AppDelegateClassName *appDelegate = (AppDelegateClassName *)[[UIApplication sharedApplication] delegate];
[appDelegate setLastUpdated:[NSDate date]];

在我设置属性之后,然后使用对viewController的引用作为参数调用以下自定义类方法:

viewController .m

ForceData *forceData = [[ForceData alloc] init];
[forceData queryServiceWithParent:self];

如果我尝试在自定义类中显示appDelegate属性的内容,它将返回一个随机值。一旦自定义类返回到viewController,appDelegate属性将保留为随机值。

我看不出有什么问题。有人可以帮忙吗?

感谢。

更新

以下是queryServiceWithParent方法中的代码:

- (void)queryServiceWithParent:(UIViewController *)controller {

    viewController = (ForcesTableViewController *)controller;
    responseData = [[NSMutableData data] retain];

    NSString *url = [NSString stringWithFormat:@"http://example.com"];
    theURL = [[NSURL URLWithString:url] retain];
    NSURLRequest *request = [NSURLRequest requestWithURL:theURL];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];

}

我仍然遇到这个问题所以非常感谢任何帮助。

更新:

我花了一些时间来看这个,我可以在queryServiceWithParent(上面)中的任何地方显示lastUpdated的内容,它显示正常。但是如果我在connectionDidFinishLoading中显示相同的属性,它将被重置。非常坚持这一个!

1 个答案:

答案 0 :(得分:1)

这个声音给我的方式是你的NSDate属性在某些时候是自动释放的,即使你的内存管理听起来很好,就像你描述它一样。

您是否可以尝试为NSDate添加额外的保留,如:

appDelegate.lastUpdated = [[NSDate date] retain]

如果这有助于我们需要有关您的代码的更多信息,以找出内存管理存在错误的位置。即appDelegate和viewController的完整标题和主文件。

相关问题