title: downloading entries from mysql database over multiple viewcontrollers
body: <p>I have a very annoying issue with code I am working on in xcode for iOS, and it is driving me crazy!</p>
  
  <p>This will take a lot of code explanation, as I need to include a lot of code.</p>
  
  <p>I had a <code>viewcontroller</code> working, connecting to an online <code>mysql</code> database, and populating a <code>tableview</code>. This was all working great.</p>
  
  <p>I then had to duplicate this code, as I want to download from another database and populate a mapview using co-ordinates from another mysql database.</p>
  
  <p><code>Screen5ViewController.h / Screen5ViewController.m</code> are the files for the <code>viewcontroller</code> with the <code>tableview</code>.</p>
  
  <p><code>ShopTable.h / ShopTable.m</code> is the code that handles the downloading of the data from the database.</p>
  
  <p>This was all working.</p>
  
  <p><code>Screen2ViewController.h / Screen2ViewController.m / MapTable.h / MapTable.m</code> are the files I duplicated to download from the other database to populate the <code>mapview</code>.</p>
  
  <p>This doesnt work at all, and doesnt even call the method <code>("NSLog(@"map table called...");")</code> - so it doesnt even try to download the data.</p>
  
  <p>I dont understand why this method isnt even called?</p>
  
  <p>The code compiles and runs with no errors and no warnings.</p>
  
  <p>Any help anyone can give would be very much appreciated. I have spent so much time looking over this code and trying everything I can think of!</p>
  
  <p>Screen5ViewController.h</p>
  
  <pre><code>#import &lt;UIKit/UIKit.h&gt;
  #import "ShopTable.h"
  
  @interface Screen5ViewController : UIViewController &lt;UITableViewDataSource, UITableViewDelegate, ShopTableProtocol&gt; {
  }
  
  @property (weak, nonatomic) IBOutlet UITableView *shopTableView;
  
  @end
  </code></pre>
  
  <p>Screen5ViewController.m</p>
  
  <pre><code>#import "Screen5ViewController.h"
  
  @interface Screen5ViewController () {
  ShopTable *_shopTable;
  NSArray *_feedItems;
  }
  @end
  
  @implementation Screen5ViewController
  
  @synthesize
  
  - (void)viewDidLoad {
  [super viewDidLoad];
  
  self.title = @"Screen 5";
  
  // Set this view controller object as the delegate and data source for the table view
  self.shopTableView.delegate = self;
  self.shopTableView.dataSource = self;
  
  // Create array object and assign it to _feedItems variable
  _feedItems = [[NSArray alloc] init];
  
  // Create new ShopTable object and assign it to _shopTable variable
  _shopTable = [[ShopTable alloc] init];
  
  // Set this view controller object as the delegate for the home model object
  _shopTable.delegate = self;
  
  // Call the download items method of the home model object
  [_shopTable downloadItems2];
  }
  
  -(void)itemsDownloaded2:(NSArray *)items {
  // This delegate method will get called when the items are finished downloading
  
  // Set the downloaded items to the array
  _feedItems = items;
  
  // Reload the table view
  [self.shopTableView reloadData];
  }
  
  #pragma mark Table View Delegate Methods
  
  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  // Return the number of feed items (initially 0)
  return _feedItems.count;
  }
  
  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  // Retrieve cell
  NSString *cellIdentifier = @"shopTableCell";
  UITableViewCell *myCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
  
  // Get the location to be shown
  Location2 *item = _feedItems[indexPath.row];
  
  // Get references to labels of cell
  
  myCell.textLabel.text = item.name;
  myCell.detailTextLabel.text = item.address;
  
  return myCell;
  }
  
  - (void)didReceiveMemoryWarning {
  [super didReceiveMemoryWarning];
  }
  
  @end
  </code></pre>
  
  <p>ShopTable.h</p>
  
  <pre><code>#import &lt;Foundation/Foundation.h&gt;
  
  @protocol ShopTableProtocol &lt;NSObject&gt;
  - (void)itemsDownloaded2:(NSArray *)items;
  @end
  
  @interface ShopTable : NSObject &lt;NSURLConnectionDataDelegate&gt;
  @property (nonatomic, weak) id&lt;ShopTableProtocol&gt; delegate;
  - (void)downloadItems2;
  @end
  
  @interface Location2 : NSObject
  @property (nonatomic, strong) NSString *latitude;
  @property (nonatomic, strong) NSString *longitude;
  @property (nonatomic, strong) NSString *name;
  @property (nonatomic, strong) NSString *address;
  @end
  </code></pre>
  
  <p>ShopTable.m</p>
  
  <pre><code>#import "ShopTable.h"
  
  @interface ShopTable() {
  NSMutableData *_downloadedData;
  }
  @end
  
  @implementation ShopTable
  
  - (void)downloadItems2 {
  NSLog(@"shop table called...");
  
  // Download the json file
  NSURL *jsonFileUrl = [NSURL URLWithString:@"http://www.example.com/shop.php"];
  
  // Create the request
  NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:jsonFileUrl];
  
  // Create the NSURLConnection
  [NSURLConnection connectionWithRequest:urlRequest delegate:self];
  }
  
  #pragma mark NSURLConnectionDataProtocol Methods
  
  - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
  // Initialize the data object
  _downloadedData = [[NSMutableData alloc] init];
  }
  
  - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
  // Append the newly downloaded data
  [_downloadedData appendData:data];
  }
  
  - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
  // Create an array to store the locations
  NSMutableArray *_locations = [[NSMutableArray alloc] init];
  
  // Parse the JSON that came in
  NSError *error;
  NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:_downloadedData options:NSJSONReadingAllowFragments error:&amp;error];
  
  // Loop through Json objects, create question objects and add them to our questions array
  for (int i = 0; i &lt; jsonArray.count; i++) {
      NSDictionary *jsonElement = jsonArray[i];
  
      // Create a new location object and set its props to JsonElement properties
      Location2 *newLocation = [[Location2 alloc] init];
      newLocation.name = jsonElement[@"title"];
      newLocation.address = jsonElement[@"description"];
      newLocation.latitude = jsonElement[@"Latitude"];
      newLocation.longitude = jsonElement[@"Longitude"];
  
      // Add this question to the locations array
      [_locations addObject:newLocation];
  }
  
  // Ready to notify delegate that data is ready and pass back items
  if (self.delegate) {
      [self.delegate itemsDownloaded2:_locations];
  }
  }
  
  @end
  
  @implementation Location2
  @end
  </code></pre>
  
  <p>Screen2ViewController.h</p>
  
  <pre><code>#import &lt;UIKit/UIKit.h&gt;
  #import "MapTable.h"
  
  @interface Screen2ViewController : UIViewController &lt;MapTableProtocol&gt; {
  }
  
  @end
  </code></pre>
  
  <p>Screen2ViewController.m</p>
  
  <pre><code>#import "Screen2ViewController.h"
  
  @interface Screen2ViewController () {
  MapTable *_mapTable;
  NSArray *_feedItems1;
  }
  @end
  
  @implementation Screen2ViewController
  
  @synthesize
  
  - (void)viewDidLoad {
  [super viewDidLoad];
  
  self.title = @"Screen 2";
  
  NSLog(@"1"); // This is called fine!
  
  // Create array object and assign it to _feedItems variable
  _feedItems1 = [[NSArray alloc] init];
  
  NSLog(@"2"); // This is called fine!
  
  // Set this view controller object as the delegate for the home model object
  _mapTable.delegate = self;
  
  NSLog(@"3"); // This is called fine!
  
  // Call the download items method of the home model object
  [_mapTable downloadItems];
  }
  
  NSLog(@"4"); // This is called fine!
  
  -(void)itemsDownloaded:(NSArray *)items {
  // This delegate method will get called when the items are finished downloading
  
  // Set the downloaded items to the array
  _feedItems1 = items;
  
  NSLog(@"%lu locations downloaded ok!", (unsigned long)_feedItems1.count);
  }
  
  - (void)didReceiveMemoryWarning {
  [super didReceiveMemoryWarning];
  }
  
  @end
  </code></pre>
  
  <p>MapTable.h</p>
  
  <pre><code>#import &lt;Foundation/Foundation.h&gt;
  
  @protocol MapTableProtocol &lt;NSObject&gt;
  - (void)itemsDownloaded:(NSArray *)items;
  @end
  
  @interface MapTable : NSObject &lt;NSURLConnectionDataDelegate&gt;
  @property (nonatomic, weak) id&lt;MapTableProtocol&gt; delegate;
  - (void)downloadItems;
  @end
  
  @interface Location : NSObject
  @property (nonatomic, strong) NSString *latitude;
  @property (nonatomic, strong) NSString *longitude;
  @property (nonatomic, strong) NSString *name;
  @end
  </code></pre>
  
  <p>MapTable.m</p>
  
  <pre><code>#import "MapTable.h"
  
  @interface MapTable() {
  NSMutableData *_downloadedData1;
  }
  @end
  
  @implementation MapTable
  
  - (void)downloadItems {
  NSLog(@"map table called..."); // THIS CODE NEVER GETS CALLED??!!
  
  // Download the json file
  NSURL *jsonFileUrl = [NSURL URLWithString:@"http://www.example.com/map.php"];
  
  // Create the request
  NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:jsonFileUrl];
  
  // Create the NSURLConnection
  [NSURLConnection connectionWithRequest:urlRequest delegate:self];
  }
  
  #pragma mark NSURLConnectionDataProtocol Methods
  
  - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
  // Initialize the data object
  _downloadedData1 = [[NSMutableData alloc] init];
  }
  
  - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
  // Append the newly downloaded data
  [_downloadedData1 appendData:data];
  }
  
  - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
  // Create an array to store the locations
  NSMutableArray *_locations = [[NSMutableArray alloc] init];
  
  // Parse the JSON that came in
  NSError *error;
  NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:_downloadedData1 options:NSJSONReadingAllowFragments error:&amp;error];
  
  // Loop through Json objects, create question objects and add them to our questions array
  for (int i = 0; i &lt; jsonArray.count; i++) {
      NSDictionary *jsonElement = jsonArray[i];
  
      // Create a new location object and set its props to JsonElement properties
      Location *newLocation = [[Location alloc] init];
      newLocation.name = jsonElement[@"firstname"];
      newLocation.latitude = jsonElement[@"mylatitude"];
      newLocation.longitude = jsonElement[@"mylongitude"];
  
      // Add this question to the locations array
      [_locations addObject:newLocation];
  }
  
  // Ready to notify delegate that data is ready and pass back items
  if (self.delegate) {
      [self.delegate itemsDownloaded:_locations];
  }
  }
  
  @end
  
  @implementation Location
  @end
  </code></pre>