0

My question is that I have one main UIViewController that allows three other UIViewControllers to be presented through it, but I am wondering if there is a way that once I dismiss one of those other three controllers, can the main UIViewController be notified or tell that it is now appearing due to the dismissal of said controller?

Thank you in advanced!

D34thSt4lker
  • 447
  • 1
  • 5
  • 12

2 Answers2

0

If your main view controller implements:

  • (void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion

and the presented view controllers send it that message, you will know since at that time it can query to see what the "presentedViewController" was.

David H
  • 40,852
  • 12
  • 92
  • 138
  • I'm sorry, I added that method to my main view controller but how am i sending the message back from the other controller? I am very unfamiliar with using completion blocks... Thanks! – D34thSt4lker Nov 19 '13 at 19:23
  • No completion block needed. From your currently visible view controller, do this to get dismissed: [self.presentingViewController dismissViewControllerAnimated:YES nil]; – David H Nov 19 '13 at 19:59
0

When you dismiss one of the three ViewControllers, you could signal to the main ViewController that they have been dismissed via a NSNotification:

    NSDictionary *viewControllerInfo = @{@"ViewControllerClass" : NSStringFromClass([self class])}
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ViewControllerDismissed" object:nil userInfo:viewControllerInfo];

And in your main viewController:

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

And respond with this method:

- (void)viewControllerDismissed:(NSNotification *)notification {
    NSDictionary *viewControllerInfo = [notification userInfo];

   //  Dictionary should be same as the one passed through the noticiation.
}

Additional note: If you are using a UIStoryboard, then you can use an unwind segue.

EDIT: Updated Dictionary to use NSStringFromClass()

Aron C
  • 838
  • 1
  • 10
  • 17
  • Sweet awesome that worked! But no, I do not use storyboards. Also, a question regarding your answer... What if I don't care which of the three view controllers are being dismissed? I just want the same effect once they are dismissed. What can I remove from there? – D34thSt4lker Nov 19 '13 at 19:37
  • Ok , nevermind on that question. I removed the NSDictionary and it will pass through. Thanks so much! – D34thSt4lker Nov 19 '13 at 19:39
  • I'm curious: If you don't care which viewController is being dismissed, couldn't you just add logic to your main viewController's viewWillAppear/viewDidAppear methods to trigger whatever action you desire after the dismiss? You could set a BOOL when you present a view controller that can be used in viewWillAppear to determine if it was triggered from dismissing a viewController or not. (Also, if satisfied with with my original answer can you mark it as accepted?) – Aron C Nov 19 '13 at 19:46
  • Yes I could but what I am trying to achieve is a custom "loading view" that is being triggered when I each controller gets presented and dismissed... If i use the main controller viewDidAppear, then this custom "loading view" will be triggered on start of application and I don't need that. Your solution allows what I need to work. – D34thSt4lker Nov 19 '13 at 19:49