0

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

2 Answers2

2

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.

Update

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):

self.itemSelectedHandler();

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.

The Mach System
  • 5,790
  • 3
  • 12
  • 20
0

Swift 3.0 version:

Popover VC:

define a property:

var dismissHandler: (() -> Void)!

call this handler when popover is being dismissed:

self.dismissHandler()

Parent VC:

pass the handler in the prepare method of the Segue

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    ...
    destinationVC.dismissHandler = {
        self.tableView.reloadData()
    }
    ...
}
cypher
  • 91
  • 1
  • 3