1

As shown in the picture above, I have two view controllers, one of which is within a navigation controller. When a button is pressed in controller A, controller B is presented through the navigation controller and is displayed modally. Is it possible for me to pass data from controller B back to controller A when the dismiss function is called on B?

Moin Shirazi
  • 4,372
  • 2
  • 26
  • 38
AlecR
  • 197
  • 1
  • 1
  • 6

3 Answers3

0

You can use delegation pattern or callbacks to do this

Andrey Volobuev
  • 862
  • 8
  • 11
  • I've tried both of those, but that would require me to present view controller B, not the navigation controller. Presenting B instead of the navigation controller doesn't work because I need to be able to present and dismiss the whole view controller, not just B. – AlecR Jan 19 '17 at 00:41
  • In that case the easiest way is to use an `unwind segue` – Andrey Volobuev Jan 19 '17 at 00:47
  • And you still can use the delegation, for example in A: `if let navigationController = segue.destination as? UINavigationController, let b = navigationController.viewControllers.first as? BViewController{ b.delegate = self }`. And calling delegate method with data before dismissing B – Andrey Volobuev Jan 19 '17 at 01:24
0

If you are using storyboards and navigating using segues, then an unwind segue will do this for you. Here is a simple tutorial that should help:

https://www.andrewcbancroft.com/2015/12/18/working-with-unwind-segues-programmatically-in-swift/

This stack overflow answer has much more detailed and valuable information on this topic

Passing Data between View Controllers

Community
  • 1
  • 1
Guillermo Alvarez
  • 1,695
  • 2
  • 18
  • 23
0

You can pass through NSNotificationCentre .

First you need to add Notification Observer and its selector in ViewControllerA as below :

[[NSNotificationCenter defaultCenter] addObserver:self
                                          selector:@selector(receiveTestNotification:) name:@"notificationName"
                                               object:nil];

-(void) receiveTestNotification:(NSNotification*)notification
{
  NSDictionary* userInfo = notification.userInfo;
  NSLog (@"%@",userInfo);
}

Now in ViewController B you need to post notification as below :

 NSDictionary* userInfo = your data ;
 NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
[nc postNotificationName:@"notificationName" object:self userInfo:userInfo];
KKRocks
  • 8,222
  • 1
  • 18
  • 84