0
votes

I have a view controller. When I press a button in it, a popover controller with a uitableview shows up. I select a row, which shows another view with some controls in it. When I press a button that says "Save Item", I want it to dismiss the popover. How do I do this?

Here's what I've tried:

  • Using the delegate and protocol pattern. This hasn't worked since in order to push another view inside my tableview, the whole thing must be embedded in a navigation view controller, so when I segue, it segues to a nav controller, not the tableview which I could set the popover delegate for.

  • Adding my main view as a member of the view I want to dismiss from. I don't know why this doesn't work.

2

2 Answers

1
votes

The Hard Clean Way

There are four view controllers in the story, plus a popover controller. I will call the three view controllers "main view controller", "nav", "vcA", and "vcB". As I understand it, "nav" is the initial content view controller of the popover and has "vcA" as its root view controller.

main view controller -> popover controller -> nav -> vcA -> (later) vcB

When you present the popover from your main view controller, you keep a reference to the popover controller. This is what makes dismissing possible, as you know.

When you create the Save button, you make its target the main view controller and its action a method in the main view controller. You will have to set this up in code; it cannot be configured from a storyboard because you cannot form an action from one scene to another. (You are able to do this because you started out with a reference to nav and vcA when you configured the popover controller initially. Thus you can hand vcA a reference to self, the main view controller. If necessary, you can then pass this reference down the chain from vcA to vcB as vcB is summoned and pushed onto the navigation stack.)

Now the user taps Save, your main view controller's method runs, and it uses its reference to the popover controller to tell it to dismiss.

The Easy Dirty Way

The heck with all that. The main view controller registers for an NSNotification. The Save button posts that NSNotification. Done. :)

The Middle Way

You could set the whole chain up in your storyboard using a popover segue, and do the dismissal through an Unwind segue matched by an unwind method back in the main view controller. I never think of this initially, because I don't like popover segues very much. But it does work.

0
votes

This is how I solved my problem (sorry for the bad english):

First, Create a property of UIStoryboardPopoverSegue in the VcA and set it from the main view controller.

Nav -> VcA_ViewController

@property (strong, nonatomic) UIStoryboardPopoverSegue *popupSegue;

Then, in the Main View Controller prepareForSegue set the property:

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:@"your segue from the mainview to the navigation"]) {
    UINavigationController *navigationController = (UINavigationController *)c;
    VcA_ViewController *vcA = (VRPointOfInterestsFiltersViewController *) navigationController.topViewController;
    vcA.popupSegue =  (UIStoryboardPopoverSegue*)segue;
} }

Now, from the VcA controller you can have the dismiss button

- (IBAction)dismissPopoup:(id)sender {
[self.popupSegue.popoverController dismissPopoverAnimated:YES]; }

Don't forget to link the popOverSegue from the MainViewController to the NavController.

Hope it helps!