Search code examples
objective-cuiapplicationdelegate

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:@"CompanyName"]) {
        // 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.


Solution

  • 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.