Dismissing Modal View Controllers

View Controller A presents View Controller B modally, which has a button to present View Controller C modally.

Here is my flow:

  • Revert to previous controller after xx amount of seconds
  • Add a UIView above all, even the navigation bar
  • How to send an NSString to another view controller
  • How to add a subview that has its own UIViewController in Objective-C?
  • ios swift 3 - UIAlertController width
  • Changing a frame in viewDidLayoutSubviews
  • A presents B which presents C

    When B presents C, I want B to be dismissed, so my only view controllers are A and C. I am not sure where to call:

    [self dismissModalViewControllerAnimated:NO];

    in order to dismiss B.

    I created a delegation pattern, where right after B presents C, A will dismiss B however nothing gets dismissed.

    If B is C’s delegate then C dismisses itself.

    4 Solutions Collect From Internet About “Dismissing Modal View Controllers”

    I think you can use NSNotificationCenter.
    You can make a NSNotificationCenter at B that calling a method to dismiss itself, then make a post Notification from C to call the NSNotificationCenter at B.

    I’m not sure of it, but it may works.

    In B add:

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

    and the method like this:

    - (void)dismissFunction:(NSNotification*)notification
          [self.navigationController dismissModalViewControllerAnimated:YES];

    And in C add:

    [[NSNotificationCenter defaultCenter] postNotificationName:@"DismissFunction" object:nil];

    You should use a UINavigationController to do this.

    *create a navigation controller with controller B as the root view controller.
    *A present the navigation controller as a modal with presentModalViewControler:animated: this will have the same affect as presenting B
    *When B needs to present C it pushes it ti the navigation view comptroller’s stack by calling [self.navigationController pushViewController:C animated:YES]
    *if C needs to be dismissed to revile B you can do those by calling [self.navigationController popViewControllerAnimated:YES]
    *If C needs to dismiss and revile A you can dismiss the modal by calling [self.navigationController.parentViewController dismissModalAnimated:YES]

    you could go 1 step further and not use a modal at all by embedding A as the root view controller of a navigation controller and pushing B to the Navigation controller instead of presenting it as a modal

    I think that you can not do it. Read again the documentation for dimissing the modal view controller

    If you present several modal view controllers in succession, and thus build a stack of modal view controllers, calling this method on a view controller lower in the stack dismisses its immediate child view controller and all view controllers above that child on the stack. When this happens, only the top-most view is dismissed in an animated fashion; any intermediate view controllers are simply removed from the stack. The top-most view is dismissed using its modal transition style, which may differ from the styles used by other view controllers lower in the stack.

    That means if you dismiss B, you will dismiss C as well

    just do this from A

    [self.navigationController dismissModalViewControllerAnimated:YES];