我試圖在各種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中顯示相同的屬性,它將被重置。非常堅持這一個!
你'合成''lastUpdated'的屬性訪問器嗎? – Anurag 2011-03-21 00:37:02
你可以顯示使用此屬性的queryServiceWithParent中的代碼嗎? – 2011-03-21 02:28:37
@Auurag - 是的,我做了syenthesize屬性,忘記在我的代碼片段中添加那一點。 – 2011-03-22 08:29:57