6

I am trying to send data from a container (child view) to the parent. In another view controller, I was able to send data from the parent to the child using

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 

However, I’m not sure how to send data from the child to the parent, specifically I’m trying to send the property userLat, which is a property of the container, to the parent. Any advice?

PinViewController.m (parent):

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.


    NSLog(@"children : %@", self.childViewControllers);
//this returns the child view controller 

NSLog(@"children : %@", self.childViewControllers.userLat);
//userLat is a property of the child view controller, but displays an error in Xcode.

}

MapWithinPinViewController (child, container)

- (void)viewDidLoad
{


    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86 longitude:151.20 zoom:6];
    mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
    mapView_.myLocationEnabled = YES;
    mapView_.delegate = self;
    self.view = mapView_;

}


 - (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate {
    NSLog(@"You tapped at %f,%f", coordinate.latitude, coordinate.longitude);

    [mapView_ clear];

    GMSMarker *marker = [[GMSMarker alloc] init];
    marker.position = CLLocationCoordinate2DMake(coordinate.latitude, coordinate.longitude);

    self.userLat = @"test";

    marker.map = mapView_;

}
sharataka
  • 5,014
  • 20
  • 65
  • 125

2 Answers2

3

Apple suggests that in your prepareForSegue, you set up the parent to be the delegate of the child. Then the child can use delegate methods (a custom protocol) to communicate back with the parent.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • I'm not sure exactly how to do that because I'm relatively new to iOS development. Do you have an example or code snippet I can look at? – sharataka Feb 25 '14 at 19:29
  • I do indeed. Take a look at this project on github: https://github.com/DuncanMC/test – Duncan C Mar 03 '14 at 22:59
0

There are several ways to accomplish this task, but unwind segues is probably the simplest. You will first need an IBActon method in your parent view controller. This method will be called when you segue from the child view controller back to the parent view controller. In your case, substitute 'userLat' for the '_data' instance variable shown below.

In ParentViewController.m add the following method...

- (IBAction)sendDataFromChildToParent:(UIStoryboardSegue *)segue
{
    ChildViewController *childViewController = segue.sourceViewController;
    _data = childViewController.data;
}

Next, open your storyboard and select the the ChildViewController. Ctrl-drag from from the button (the one you are using to navigate back to the ParentViewController) to the 'Exit' button (square with an arrow coming out of it to the right) in the dock just below your view controller. A pop up menu will show. Select 'sendDataFromChildToParent' under 'Selection Segue'. Keep in mind this will only work if you are NOT using a NavigationController's back button to segue back to the parent as the navigation bar back button will not call the method above. There may be a way to have the back button call the method, but I have not looked into it.

In your ChildViewController be sure that you are setting your 'data', or in your case 'userLat' property, before navigating back to the ParentViewController. You can use the following method in your ChildViewController to perform any last minute setup before navigating back to the ParentViewController. Although from the looks of your code, you will not need it since 'userLat' is set in your mapView method.

- (void)prepareForSegue:(UIStoryboard *)segue sender:(id)sender
{
     // Code for preparation before segue back to parent.
}

If your view controllers are imbedded in a navigation controller and you want the data to be sent from the child to the parent when the back button is pressed, there is another approach that can be used. But from your question, I wasn't sure if this was the case for you.

ninefifteen
  • 931
  • 1
  • 10
  • 16
  • You mentioned, "Ctrl-drag from from the button" however I don't have a button. I'm using the google maps sdk and the didTapAtCoordinate is just when the user taps on the map. Does this change how I would implement this? – sharataka Feb 27 '14 at 14:56
  • Also, I'm using Navigation Controller – sharataka Feb 27 '14 at 14:57
  • So, what action triggers the navigation back to the parent view controller? I am assuming the didTapAtCoordinate method? – ninefifteen Feb 27 '14 at 15:40