Sammy Sammy - 3 years ago 97
iOS Question

Update the first view after dismissing Popover

I segue from the first view controller(tableview) to the second view controller which is a tableview. After I am dont with the second view, I need to reload table view in the first controller.
popoverPresentationControllerDidDismissPopover gets called if I tap outside the popover but what if there is another button in the second tableview which segues back to the first one?
Normally I need user to tap Add button to come out of tableview rather than tapping outside the popover. I reserve the outside tap as the cancel action.

Thank you
Poppver on top of a tableview

Answer Source

The delegate method popoverPresentationControllerDidDismissPopover does not get called when you dismiss it programmatically.

The solution is when you dismiss the popover by calling dismiss(animated:completion:), you have to call the table view to reload data too.

So basically you call tableView.reloadData() twice. One in the popoverPresentationControllerDidDismissPopover, one after dismiss(animated:completion:) which is in your add button tapped method.


From you screenshot, I assume your first view controller (VC1) is the one on background, the second view controller (VC2) is the pop over.

If so, you have two options:

  1. On VC2, declare a custom protocol to notify VC1 to reload table view data whenever a row is selected and set delegate in VC1 to VC2.
  2. Instead of using a protocol, you can simply declared a block property in VC2. For example:

    @property (copy, nonatomic) void (^itemSelectedHandler)();

And when an item is selected call this (still in VC2):


Then in VC1, after initialising VC2, which is the popover, you handle the block. For example:

vc2.itemSelectedHandler = ^{
    [vc1.tableView reloadData];

Unfortunately, I'm not experienced with swift programming so I have to use objective c code as an example.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download