views:

126

answers:

1

I have a class (DataImporter) which has the code to download an RSS feed. I also have a view and separate class (TableView) which displays the data in a UITableView and starts the parsing process, storing parsed information in an NSMutableArray (items) which is located in the (TableView) subclass.

Now I wish to add a UIMapView which displays the items in the (items) NSMutableArray. Herein lies the issue - I need to somehow get the data from the (items) NSMutableArray into the new (mapView) subclass which I'm struggling with - and I preferably don't want to have to create a new class to download the data again for the mapView class when it already is in the applications memory. Is there a way I can transfer the information from the NSMutableArray (items) class to the (mapView) class (i.e. how do I declare the NSMutableArray in the (mapView) class)?

Here's a overview of how the system works: App opened> Data downloaded (using DataImporter class) when (TableView) viewDidLoad runs> Data stored in NSMutableArray accessible by the (TableView) class> And from here I need to access and declare the array from a new (mapView) class.

Any help greatly appreciated, thanks.

Code for viewDidLoad MapKit:

Data *data = nil;
NSString *ilocation = [data locations];
NSString *ilocation2 = @"New Zealand";

NSString *inewlString;
inewlString = [ilocation stringByAppendingString:ilocation2];
NSLog(@"inewlString=%@",inewlString);

if(forwardGeocoder == nil)
{
    forwardGeocoder = [[BSForwardGeocoder alloc] initWithDelegate:self];
}   

// Forward geocode!
[forwardGeocoder findLocation: inewlString];

Code for parsing data into original NSMutable Array:

- (void)beginParsing {
    NSLog(@"Parsing has begun");
    //self.navigationItem.rightBarButtonItem.enabled = NO;
    // Allocate the array for song storage, or empty the results of previous parses
    if (incidents == nil) {
        NSLog(@"Grabbing array");
        self.datas = [NSMutableArray array];
    } else {
        [datas removeAllObjects];
        [self.tableView reloadData];
    }
    // Create the parser, set its delegate, and start it.
    self.parser = [[DataImporter alloc] init];      
    parser.delegate = self;
    [parser start];
}
A: 

Since you have a property self.datas you can get a reference to the data just by calling that from your UIMapView, or by assigning to some target property in your map view from whatever object controls both views:

mapView.someArrayProperty = tableView.datas

However, you might want to rethink your overall program structure. Keeping what is in effect model data inside views is not good practice and tends make things pretty spaghetti-like as you add more objects and functionality.

walkytalky
Hmm I am getting a warning - "incompatible objective c types assigning 'stuct nsmutablearray *' 'expected struct NSString *' that displays when the app runs. Not working either. Any ideas? Code I'm attempting to use is mapView= tableView.datas. With the someArrayProperty you mention above, where does that come from? Does it need to be declared in the .h file for mapView?
Graeme
@Graeme Yes, you need to declare the property in the header file (with type `NSArray*` or `NSMutableArray*`, and attributes `readwrite` and `retain`) and also `@synthesize` it in the implementation.
walkytalky
Have added NSMutableArray *mapData and the attributes and synthesized it, but now getting "accessing unknown setMapData: class method", and "object cannot be set - either readonly property or no setter found" errors on the code "mapView.mappedIncidents (the new NSMutableArray) = tableView.datas;" Any ideas what might be wrong?
Graeme
Updated the code to "mappedIncidents = currentTable.incidents;" - but now get a notice saying 'NSMutableArray may not respond to '-name' (which is a row of information in the array). Any thoughts?
Graeme
@Graeme: Not meaning to be harsh, my main thought is that you need to take a step back from randomly changing lines of code and instead think a bit harder about how your program is structured, what all these objects *are* and what the calls to them are doing. The arrays are just containers for data. The views are things that display data. Your controller(s) should be in control. For this particular error, an array is not an object with fields like "name", it's just a list of values. You will have to work out which is which from its position (or choose a more appropriate data structure).
walkytalky
When you say the attributes should be readwrite and retain - is that for the original NSMutableArray, or the mapView array? Thanks.
Graeme
@Graeme: Well, both. The `readwrite` is so that you can assign to it. The `retain` is so that it keeps a claim on the object's lifetime. You should read the docs on [properties](http://developer.apple.com/mac/library/documentation/cocoa/conceptual/objectivec/articles/ocProperties.html) and [object ownership](http://developer.apple.com/mac/library/documentation/cocoa/conceptual/MemoryMgmt/Articles/mmObjectOwnership.html). Twice.
walkytalky