Search code examples
objective-ciosxcodedata-transfer

Transferring Data Back to Original View


So I am able to transfer the data from the first view to the second view like this:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
   if ([segue.identifier isEqualToString:@"Check Mark Segue"])
      {
        NSLog(@"Transfering Data");
        AutoRenewDrop *controller = segue.destinationViewController;
        controller.transferData = self.renewDate.text;
      }
}

However, I try to transfer a new value back to renewDate.text when the user hits done and the transferData is working correctly but the renewDate.text does not change. Here is the code that I am using to transfer the data back:

-(IBAction)done:(UIStoryboardSegue *)segue {

[self.navigationController popViewControllerAnimated:YES];
AddR *add = [[AddR alloc] init];
add.renewDate.text = transferData;

}

Can someone tell me how to fix this?


Solution

  • You need to add a property that contain a reference of the first view into the second view :

    @interface AutoRenewDrop
    @property(weak, nonatomic) AddR *callerView;
    @end
    

    And then in the done method of the second view you can just update the variale in the caller view :

    -(IBAction)done:(UIStoryboardSegue *)segue {
    [self.navigationController popViewControllerAnimated:YES];
    callerView.renewDate.text = transferData;
    }
    

    Of course when you instantiate the second view you will have to set the reference, in this way :

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
       if ([segue.identifier isEqualToString:@"Check Mark Segue"])
          {
            NSLog(@"Transfering Data");
            AutoRenewDrop *controller = segue.destinationViewController;
            controller.transferData = self.renewDate.text;
            controller.callerView = self; //Here, you are passing the reference to this View
          }
    }