1

I am new to iOS app development.

I am now having several views like in the picture below (not exactly the case), in which I could get to the third view (rightmost) through 2 subsequent segues from the first view.

EDIT: Supplementary Information: The third view is actually linked to a container in the second view. And it is a modal segue from the first to second.

enter image description here

And in the third view, I have a button which could bring me back to the first view by the dismissViewControllerAnimated:completion: method. But how could I pass some data back to the first view?

I just know how to pass data between adjacent views using method like prepareForSegue:sender:. But I'm not sure how to do it across more than 1 view.

Should I use Delegates or Singleton or there are other better options? Thanks in advance.

d.yuk
  • 809
  • 1
  • 12
  • 30

3 Answers3

1

There are following ways for doing that.

  • Delegates is one way to pass data back to your child.
  • Blocks is another very simple approach.
  • Notifications can also be used.
  • Unwind segue is a new addition to this
  • I would suggest Singleton at the end.
Abubakr Dar
  • 4,078
  • 4
  • 22
  • 28
0

Suppose you want to transfer some string .You can pass the data like this :

  UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
   ViewController *controller = [storyboard instantiateViewControllerWithIdentifier:@"vc"];
    controller.string = "some string";
    [self presentViewController:controller animated:YES completion:nil];

If you want to right back to previous view controller then you can use completion handler :

  ViewController *vc = (ViewController*)self.presentingViewController;
 [self dismissViewControllerAnimated:YES completion:^{
    vc.string = @"some string";
  }];
V-Xtreme
  • 7,230
  • 9
  • 39
  • 79
0

See the correct answer of this question:

Passing Data between View Controllers

This question is been asked frequently, remember that you can also use an Unwind segue from the third to the first viewController and use :

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

if ([segue.identifier isEqualToString:@"passDataFromThirdToFirst"]) {

 //Pass the data here with the methods that suggest 
 //in the correct answer I've linked you.
}  }
Community
  • 1
  • 1
Luis Mejías
  • 301
  • 2
  • 10