1

I have a view controller VC1 and I am doing following two operations on it:

  1. Push another view controller VC2 from VC1's navigation controller by calling

[VC1.navigationController pushViewController: animated:YES];

  1. Present another view controller VC3 from VC1 by calling.

    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:VC3]; [VC1 presentViewController:navController animated:YES completion:nil];

Now when I am coming from VC2 to VC1 I am calling

[VC2.navigationController popViewControllerAnimated:YES];

and from VC3 to VC1 I am calling

[VC3.navigationController dismissViewControllerAnimated:YES completion:nil];

My question is when I am coming back to VC1 how do I know that whether I am coming from VC2(by dismissing) or VC3(by popping)?

Note: VC1 is loaded as child view controller inside a parent view controller VC4.

Rishil Patel
  • 1,977
  • 3
  • 14
  • 30
atul
  • 168
  • 1
  • 14
  • 1
    "My question is when I am coming back to VC1 how do I know that whether I am coming from VC2(by dismissing) or VC3(by popping)?" The very question is a bad smell. You should be asking yourself why you think you need to know that. – matt Oct 29 '15 at 20:48
  • In reply to matt's comment: Because I need to update my VC1's UI elements in different ways depending on from where it came back. – atul Oct 29 '15 at 20:55
  • No. The presented view controller or the pushed view controller, _they_ should update whatever needs updating, so that when VC1 gets `viewWillAppear:` it knows exactly what to do based on its own properties. Or, even better, VC1 has two methods, one of which will be called by the presented v.c., and the other which will be called by the pushed v.c. It's up to you to prepare your communications better. Don't use things like "I am appearing because the pushed view controller was popped" to avoid doing that job correctly. – matt Oct 29 '15 at 20:56

1 Answers1

1

The best way would be to have the childViewController call back to the presenting view controller. By doing this the childViewController will not need to know the implementation details of whether it was presented modally or in a navigation stack etc.

With blocks it would look something like

@interface VC2 : UIViewController

@property (nonatomic, copy) void (^completion)(VC2 *viewController);

@end

You would set this block up something like this

VC2 *viewController = VC2.new;
viewController.completion = ^(VC2 *viewController) {
  [viewController.navigationController popViewControllerAnimated:YES];
};

[VC1.navigationController pushViewController:viewController animated:YES];

Now where you was previously calling

[VC2.navigationController popViewControllerAnimated:YES];

You instead call

self.completion(self);

You put any logic you want to be associated with coming back from a specific viewController inside the completion handler

Paul.s
  • 38,494
  • 5
  • 70
  • 88