Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send data from container to parent view controller in iOS? [duplicate]

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_;

}
like image 611
sharataka Avatar asked Oct 20 '22 13:10

sharataka


1 Answers

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.

like image 92
Duncan C Avatar answered Oct 23 '22 02:10

Duncan C