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_;
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With