Question

I am passing an NSArray that contains objects to a screen where the object properties are displayed in a table. Each of these objects contain a latitude property, and a longitude property. I would like to implement a functionality where the user selects a cell (where each cell represents an object from the NSArray), and the user is then taken to another screen where they can see an annotation representing the location of the object on a map, and an annotation representing the user. How do I do this? Here is my relevant code from my RootViewController.m class:

SecondViewController *controller = [[SecondViewController alloc] initWithNibName:@"SecondView" bundle:[NSBundle mainBundle]];
        self.secondViewController = controller;
        [controller release];

        self.secondViewController.locationList = sortedLocations;

        [[self navigationController] pushViewController:controller animated:YES];

My relevant code in SecondViewController.m looks like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"locationcell";

LocationTableViewCell *cell = (LocationTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[LocationTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

Location *location = [locationList objectAtIndex:indexPath.row];
cell.locationName.text = location.name; 
cell.locationAddress.text = location.address;
cell.locationDistance.text = location.distance;

return cell;
}

Please bear in mind that the visible properties are name, address, and distance, but the location object also contains latitude, and longitude properties. I know I have to create a new screen called MapViewController. But as I said, I really am not sure where to go from the table on the screen, to a map that shows the location object, and the user.

Was it helpful?

Solution

Create a new ViewController with MapView and pass the latitude and longitude value to the New ViewController.

Assume that you have create the view Controller as MyLocationMapViewController.

In SecondViewController.m implement the following

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    Location *location = [locationList objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:[[MyLocationMapViewController alloc] initWithLocation:location] animated:YES];
}

in MyLocationMapViewController.h

  • (id)initWithLocation:(Location *)location;

in MyLocationMapViewController.m

  • (id)initWithLocation:(Location *)location { *locationObj = location;

}

Now you can use the locationObj for getting the map values and display the coordinates in MapView.

OTHER TIPS

create MapViewController object in secondview controller

write the below code in your secondview controller

  • (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    MapViewController *mapView = [[MapViewController alloc] initWithNibName:@"MapViewController" bundle:[NSBundle mainBundle]];

    self. mapView = controller;
    [mapView release];
    
    
    self.mapView.latit = location.latitude;
    self.mapView.longi = location.longitude;
    [[self navigationController] pushViewController:controller animated:YES];
    

    }

create 2 double variables called latit and longi in MapViewController to store latitude and longitude

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top