2011-03-20 32 views
1

我試圖在各種viewControllers之間共享數據,我使用的appDelegate中聲明的屬性。我在我的第一個viewController中設置屬性,當我打印出內容時,一切看起來都很好,但是在我調用一個自定義類方法之後,該屬性被設置爲一些隨機值,每當我運行該應用程序時該值都會改變。請參見下面的代碼:AppDelegate屬性被重置viewControllers

的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屬性保持爲隨機值。

我看不到發生了什麼問題。誰能幫忙?

謝謝。

UPDATE

這裏是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]; 

} 

我仍然有這個問題,所以任何的幫助深表感謝。

UPDATE:

我花了一些時間在看這個,我可以顯示內queryServiceWithParent(正上方)的任何地方LASTUPDATED的內容,它顯示的罰款。但是如果我在connectionDidFinishLoading中顯示相同的屬性,它將被重置。非常堅持這一個!

+0

你'合成''lastUpdated'的屬性訪問器嗎? – Anurag 2011-03-21 00:37:02

+0

你可以顯示使用此屬性的queryServiceWithParent中的代碼嗎? – 2011-03-21 02:28:37

+0

@Auurag - 是的,我做了syenthesize屬性,忘記在我的代碼片段中添加那一點。 – 2011-03-22 08:29:57

回答

1

對我來說聽起來的方式是,你的NSDate屬性在某些時候是自動釋放的,即使你的內存管理聽起來不錯,你描述它的方式。

你可以嘗試添加一個額外保留你的NSDate,如:

appDelegate.lastUpdated = [[NSDate date] retain]

如果這能幫助我們不需要知道你的代碼的更多信息,找出你的內存管理有錯誤。即appDelegate和viewController的完整頭文件和主文件。

+0

不好意思啊,我設法解決另一個論壇上這個問題。但是,你是對的,日期是被釋放。 – 2011-04-07 20:37:59