1

UPDATE: Tried the links you suggested, but can't get it to work.

Just to confirm I'm understanding it right, these are the steps I took.

First view controller is called FirstViewController, second is called SearchViewController.

  1. Import SearchViewController into FirstViewController, and add
  2. In FirstViewController.m, add the following: SearchViewController *svc =[[SearchViewController alloc] init]; svc.delegate =self;

  3. In SearchViewController.h I added:

    @protocol SearchViewControllerDelegate

    • (void)setLat:(CLLocationDegrees)lat setLon:(CLLocationDegrees) lon;

as well as the delegate property:

@property (assign) id <SearchViewControllerDelegate> delegate;
  1. In SearchViewController.m I synthesized delegate, and added [self.delegate setLat:51.0000 setLon:-0.10000];

From some NSLog testing it seems that the method setLatSetLon method itself is never called.

I'm currently trying to finish off my university iPad application. The purpose of it is to provide a live google/apple maps view of the campus with overlays of annotations and MKPolygons. This all works fine.

I've now added a button that loads a popover (the second view controller) with a table of the campus buildings (which are themselves objects of CampusBuilding, where the coordinates are stored) as well as a search box, and this is all fine. What I'm looking to do is when the user selects a row, the map view centres on that specific building. I've written a method below which works fine when I reference it from the maps view controller itself.

But how would I do this from the second view controller?

Here's the method that is present in the first view controller:

- (void)setLat:(CLLocationDegrees)lat setLon:(CLLocationDegrees) lon{

MKCoordinateSpan span;
span.latitudeDelta = (double) .003;
span.longitudeDelta = (double).003;
//Define the default region to focus on
MKCoordinateRegion region;
region.span=span;
region.center=CLLocationCoordinate2DMake
(lat,lon);
//set the default region to 'region'
[_mapView setRegion: region animated:YES];
[_mapView regionThatFits:region];

}

Now in the second view controller, it works fine as a table view controller. I've set it up so when the user selects a row it loads a new view. I also tried to initiate an object of the first view controller and then wrote this: FirstViewController* firstVC;

    [firstVC setLat:building.latValue setLon:building.lonValue];

It all compiles but no movement happens, I assume because I'm initiating a new object, rather than referring to the one that is currently active.

Is there a way to do this? Comments on how I can improve the code are always welcome, I'm relatively new to this.

Thanks

Mo Moosa
  • 937
  • 2
  • 19
  • 39

1 Answers1

1

You were right that firstVC will be a new instance of FirstViewController. Not the previous one.

What you need is to use Objective-C's delegate pattern. It is commonly used in this scenario. Here are two examples from my previous SO answers that have sample code on how to implement.

how to resume timer when poping to view2

Calling a method from another class in Objective C

Community
  • 1
  • 1
user523234
  • 14,323
  • 10
  • 62
  • 102
  • Excellent, thank you. I was thinking it might be something to do with delegates but I still don't really understand delegating properly. I'll have a look at the links, and I anticipate my understanding of delegation will only improve with experience. Thanks for the answer. – Mo Moosa Jul 21 '12 at 17:30
  • Thanks for the help, sadly I cant get it to work. Could you look at the update in the original post, please? – Mo Moosa Jul 21 '12 at 23:58
  • I think you missed one more step: In your FirstViewController's interface file - the .h file, make sure you have included this , like so: @interface FirstViewController : UIViewController – user523234 Jul 24 '12 at 00:47
  • I did, like so: @interface FirstViewController : UIViewController { When I have a bit more time I'll have to set up a new project to test it. I've read up on it more and your solution is making more sense so I'll accept this answer. Thanks – Mo Moosa Jul 24 '12 at 20:52