44

I am having a problems with the dismissViewControllerAnimated method not closing down the view.

What is happening in the app here is:

  • Cell in ItemViewController is selected.
  • View is pushed to ItemDetailViewControllerand details are sent through a delegate
  • User selects 'done' and the event is sent via a delegate to be closed in ItemViewController

All of this works except for the View is not dismissed, there are no errors. Can anyone see what is wrong?

- (void)itemDetailViewControllerDidFinish:(ItemDetailViewController *)controller
{
    NSLog(@"Controller: %@", controller);
    // Returns - Controller: <ItemDetailViewController: 0x6b68b60>

    [self dismissViewControllerAnimated:YES completion:nil];
}
pkamb
  • 33,281
  • 23
  • 160
  • 191
Ad Taylor
  • 2,775
  • 5
  • 27
  • 32

5 Answers5

66

What if you call [controller.navigationController popViewControllerAnimated:YES] instead?

For that matter, what if you call [controller dismissViewControllerAnimated:YES completion:nil] instead of calling it on self?

Nick Lockwood
  • 40,865
  • 11
  • 112
  • 103
10

The answer is in this page: dismissviewcontrolleranimated-vs-popviewcontrolleranimated

dismissViewController is used when you do not have a navigationcontroller. Most probably you are using a navigation controller, then use self.navigationController popViewController instead.

Also take note of lemax his remark: use NULL, not nill for the completionhandler

Community
  • 1
  • 1
Leander
  • 612
  • 7
  • 8
5

I had a problem in iOS5 where the standard completion callback was not allowing the view to completely dismiss (only the current pushed view of that modal)

[controller dismissViewControllerAnimated:YES completion:^ {
     //
 }];

Solution for iOS5 is to not have a callback:

[controller dismissViewControllerAnimated:YES completion:nil];
Danoli3
  • 3,203
  • 3
  • 24
  • 35
4

Had a problem where calling dismissViewControllerAnimated dismissed the keyboard in a UIViewController, but not the view itself.

Solved it by using two calls:

[self dismissViewControllerAnimated:NO completion:nil];
[self dismissViewControllerAnimated:YES completion:nil];

an instant one for the keyboard, then an animated one for the controller

Cbas
  • 6,003
  • 11
  • 56
  • 87
0

Your Situation is - ItemViewController -> ItemDetailViewController (pushed on navigationController) Self.dismissViewController(..) dismiss a view controller that is presented over self(in ur case it is ItemViewController). Here, u did not presented any VC over self, instead u pushed a new VC over navigation stack. So, Correct way to dismiss ItemDetailViewController would be

self.navigationController.popViewController(true). please read the description of dismissViewController(....) to get more clarity.

Arya
  • 13
  • 3