1

I have TableViewController & ViewController.

Airports.h

@interface Airports : UITableViewController

@property(strong, nonatomic) NSNumber* latitude;
@property(strong, nonatomic) NSNumber* longitude;
@property(strong, nonatomic) NSString* selectedAirportName;

@end

Airports.m

Here is an instance that sets latitude, longitude & selectedAirportName properties.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *airportNames = airportsArray[indexPath.row];
    self.selectedAirportName = [airportNames valueForKey:@"$"];
    NSArray *airportCoordinates = airportsCoordinates[indexPath.row];
    self.latitude = [airportCoordinates valueForKey:@"Latitude"];
    self.longitude = [airportCoordinates valueForKey:@"Longitude"];
}

Row selection forwards user to ViewController where I have an instance that shows GoogleMap with marker accordingly to information in selected row. How to give that instance access to latitude, longitude & selectedAirportName properties from Airports class? Actually, I wonder how can I make that properties public to operate with them out of didSelectRowAtIndexPath instance and out of Airports class.

I'm only a beginner. So don't shoot me :)

2
  • There are so many quesiton on this topic. Look for passing data via segues. Commented Aug 31, 2016 at 15:15
  • 1
    It might be the most common question in the iOS tags (see here, also stackoverflow.com/a/9736559/294949). I'd ask the others giving answers and links here to consider why it is asked and re-asked so often. I think the reason is that the answers given are too often about the specific mechanics of vc communication, and not enough about the idea of MVC as practiced in iOS. Commented Aug 31, 2016 at 15:21

2 Answers 2

1

You should create a seperate object that maps the data you wish to pass to the viewcontroller with a google map, maybe call it something like Airport.

Then when you push the segue from the first view controller(Airports), you can pass this object to the view controller in the prepareforsegue method.

How to pass prepareForSegue: an object

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. So, when I go to ViewController TableViewController disappears with all properties? Got it. But NSLog doesn't show anything when I try to print latitude & longitude values with different instance in Airports.m
Hooray for this. The separate object is can be considered the state of the application, it's raison d'être, also known as a "model".
0

In your ViewController.h have it look like this:

#import <UIKit/UIKit.h>


@interface ViewController : UIViewController

@property NSNumber* airportLatitude;
@property NSNumber* airportLongitude;

@end

Then in your Airports.m, when you instantiate your ViewController, you can set the values of ViewController like this:

ViewController *view = [[ViewController alloc] init];

view.airportLatitude  = [NSNumber numberWithInt:1];
view.airportLongitude = [NSNumber numberWithInt:1];

2 Comments

I can't disagree, but will nitpick a little and say (a) that view is a bad name for a view controller instance, and (b) that suggesting allocation of a VC like this invites the reader to make the most common mistake (which is to segue to a different instance of that vc), and (c) that a principled answer should at some point contain the word "model", and include the idea that vcs should be thought of as inspectors and editors of a shared model.
I'll take that in and consider it, then do my best version of whatever I think that would be.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.