开发者

AppDelegate property being reset between viewControllers

开发者 https://www.devze.com 2023-02-17 20:35 出处:网络
I\'m trying to share data between various viewControllers, I\'m using a property declared in the appDelegate. I set the property in my first viewController and when I print out the contents, everythin

I'm trying to share data between various viewControllers, I'm using a property declared in the appDelegate. I set the property in my first viewController and when I print out the contents, everything looks fine, but after I call a custom class method, the property gets set to some random value that seems to change every time I run the app. See code below:

appDelegate .h

NSDate *lastUpdated;

@property (nonatomic, retain) NSDate *lastUpdated;

viewController .m

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

After I set the property, I then call the following custom class method with a refe开发者_如何学Crence to the viewController as a parameter:

viewController .m

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

If I try and display the contents of the appDelegate property within my custom class, it returns a random value. Once the custom class returns to the viewController, the appDelegate property stays as the random value.

I can't see what's going wrong. Can anyone help?

Thanks.

UPDATE

Here is the code within the queryServiceWithParent method:

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

}

I'm still having this problem so any help is much appreciated.

UPDATE:

I've spent some more time looking at this and I can display the contents of lastUpdated anywhere within the queryServiceWithParent (just above) and it displays fine. But if I display the same property in the connectionDidFinishLoading, it's reset. Very stuck with this one!


The way this sounds to me is that your NSDate property is autoreleased at some point, even though your memory management sounds fine the way you describe it.

Can you try to add an extra retain to your NSDate, as in:

appDelegate.lastUpdated = [[NSDate date] retain]

If that helps than we need more information about your code to find out where your memory management has errors. i.e. the complete header and main file of your appDelegate and viewController.

0

精彩评论

暂无评论...
验证码 换一张
取 消