我试图在各种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