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.

8
  • You want to send data from third to first? Commented Feb 4, 2015 at 4:37
  • @AbubakrDar Yes, I would have to obtain some data from the third, and pass it back to the first, without going through the second one. Commented Feb 4, 2015 at 4:37
  • in this current situation i would think a delegate callback would be most appropriate, but a singleton can work as well if you need to keep the state of the system as a whole, really up to you, all depends on the problem Commented Feb 4, 2015 at 4:48
  • What kind of data? There may be different ways to design this. Commented Feb 4, 2015 at 4:50
  • You can take any of the given approach really, but avoid using _singleton for this purpose only. Commented Feb 4, 2015 at 4:50

3 Answers 3

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.
Sign up to request clarification or add additional context in comments.

Comments

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";
  }];

Comments

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.
}  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.