How to send data from a container to a parent view controller in iOS?

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 parent to child using

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

However, I'm not sure how to send data from the child to the parent, in particular, I'm trying to send the userLat property, which is a container property, to the parent property. Any tips?

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

}
+4
source share
2 answers

Apple , readyForSegue . ( ) .

+3

, segues, , . IBActon . , . 'userLat' '_data', .

ParentViewController.m ...

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

ChildViewController. Ctrl-drag (, ParentViewController) "" ( , ) - . . "sendDataFromChildToParent" " ". , , NavigationController back, , . "" , .

ChildViewController , ParentViewController userLat. ChildViewController , ParentViewController. , , mapView userLat.

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

, "", , . , .

0

Source: https://habr.com/ru/post/1528819/


All Articles