0

I have a tableview controller, a detail VC. and an edit detail VC that allows for deletion of an item. The Tableview is fed by an array of items.

Upon deletion of the item, I would like not only to dismiss the edit VC but also the detailVC underneath it (since the item no longer exists) leaving just the updated tableview.

I can successfully update the tableview with a notification and I can dismiss the edit view controller. However, I can't figure out a way to delete the detail VC after the edit VC disappears.

So far, after the deletion is confirmed from the server in the editVC I have:

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

This dismisses the editVC but not the detail VC.

I also send a notification upon delete that is observed by both the tableview VC and the detail VC and the handler for both has similar dismissal code. I've tried in each:

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

But the detail View still does not get dismissed.

Of note, the detail is a Show (e.g.) push and the Edit is a modal view controller embedded in its own nav.

VCs in Storyboard. The First nav is part of a TabViewController in the main storyboard

Edit:

If it makes any difference, the delete method in the third (edit) VC is called from an AlertViewController.

From Apple's documentation, merely calling dismissViewController should dismiss all later VCs in the navigation stack but that does not seem to be happening:

Discussion

The presenting view controller is responsible for dismissing the view controller it presented. If you call this method on the presented view controller itself, UIKit asks the presenting view controller to handle the dismissal. If you present several view controllers in succession, thus building a stack of presented 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. If you want to retain a reference to the view controller's presented view controller, get the value in the presentedViewController property before calling this method. The completion handler is called after the viewDidDisappear: method is called on the presented view controller.

enter image description here

2
  • is it possible for you to post your storyboard. It's not clear how you have arranged your view controller hierarchy . Commented Apr 7, 2020 at 16:51
  • Please see storyboard above Commented Apr 7, 2020 at 20:21

1 Answer 1

0

You are presenting a navigation controller from DetailVC. That's why your code doesn't work. You can go back to tableview scree from EditVC using following code.

UINavigationController *mainNavigation = (UINavigationController *) self.navigationController.presentingViewController;
[self dismissViewControllerAnimated:YES completion:^{
    [mainNavigation popViewControllerAnimated:YES];
}];

Place above code inside EditVC, this will do the trick.

Sign up to request clarification or add additional context in comments.

3 Comments

This makes sense but did not work. When I set a breakpoint and look at mainNavigation it shows a picture of the detail View. However, when I do, it shows MainTab. po parentViewController, gives a UINavigationController. po Self gives EditVC. po Self.presentingViewController also shows MainTab. I should have mentioned that the first TableViewVC is part of a Main Tab. Will update pic above
Yes. I have also tried about every variation you can think of. I put your code in the edit VC and in the table VC triggered by the notification and it had the same effect. Also reversed order, same effect. I've tried presentingViewController, parentViewController etc with every possibility in completion block. Ready to give up and just show a deleted label on the deleted detailVC. Really don't understand why Apple would make something so simple so complicated.
Just realized, one other wrinkle is I call the method to delete the item (and dismiss VC) in EditVC from an alertViewController. Maybe the AlertVC counts as the first VC and I need to actually delete three in a row. However, I have also tried calling dismissViewController, three, four and five times to no avail.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.