updating a variable in appDelegate
In the application delegate, I created an array for multiple view controllers to reference. However, when I try to update the array in one view, it comes up empty in the other.
View 1:
MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
dataTempForSearch = appDelegate.myData;
I then have an xml parser that inserts an object into the array.
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:@"Compan开发者_如何学PythonyName"]) {
// save values to an item, then store that item into the array...
[item setObject:currentTitle forKey:@"title"];
[item setObject:currentAddress forKey:@"address"];
[item setObject:currentCity forKey:@"city"];
[item setObject:currentCountry forKey:@"country"];
[dataTempForSearch addObject:[item copy]];
}
}
This returns everything fine in view 1, but in view 2 I have:
- (void)viewDidLoad {
MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
dataTempForMap = appDelegate.myData;
NSLog(@"myData appDelegate from: %@", dataTempForMap);
}
when this second view is loaded, the dataTempForMap
returns an empty array.
It's possible that you are reinitializing the array over the original populated one. I'd put a breakpoint on the spot where you're doing
myData = [[NSMutableArray alloc] init];and see if it's being called more often then you thought.
I suspect that you have not initialized the myData array in the delegate. You should be calling
[[NSMutableArray alloc]init];
in your
- (void)applicationDidFinishLaunching:(UIApplication *)application {
method. Otherwise the pointer you are getting back from the delegate is not pointing to an instance of NSMutableArray
I would then check with the debugger that you are not getting nil back and that you are getting the same address back. If either of these are not true I suspect that order in which things is happening is the problem
精彩评论