Search code examples
ios5uitableviewuipopovercontrollerxcode4.3

Can't close popover after selecting item within table view


I am creating and iPad application using XCode version 4.3.2. I am having trouble figuring out how to close a popover that is created in a storyboard.

On my main screen I have a button. On the storyboard, I have a segue defined from that button to my popover. My popover is a table view controller. After selecting an item in the popover table view, I am sending the selected information back to the parent and attempting to close the popover. Everything works except I cannot get the popover to close.

The code for the main screen .m file:

#import "SectionViewController.h"
#import "SortByTableViewController.h"

@interface SectionViewController () <SortByTableViewControllerDelegate>
@end

@implementation SectionViewController

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"DisplaySortByOptions"]) 
    {
        SortByTableViewController *popup = (SortByTableViewController*)segue.destinationViewController;
        popup.selectedSection = self.selectedSection;
        popup.receivedOption = self.selectedItemCharacteristic;
        popup.delegate = self;
    }
}

- (void)sortByTableViewController:(SortByTableViewController *)sender 
                           returnedOption:(ItemCharacteristic *)returnedOption
{
    if(!returnedOption)
    {
        [self.sortByButton setTitle:@"SHOW ALL" forState:UIControlStateNormal]; 
    }
    else 
    {
        [self.sortByButton setTitle:returnedOption.name forState:UIControlStateNormal];
    }
    self.itemCharacteristic = returnedOption;
    [self dismissViewControllerAnimated:YES completion:nil]; //THIS DOES NOT CLOSE THE POPOVER
}

The code for the popover .h file:

#import <UIKit/UIKit.h>

@class SortByTableViewController;

@protocol SortByTableViewControllerDelegate <NSObject>

- (void)sortByTableViewController:(sortByTableViewController *)sender 
                           returnedOption:(ItemCharacteristic *)returnedOption;

@end

@interface SortByTableViewController : UITableViewController

@property (nonatomic, strong) Section *selectedSection;
@property (nonatomic, strong) ItemCharacteristic *receivedOption;
@property (nonatomic, weak) id <SortByTableViewControllerDelegate> delegate;

@end

The code for the popover .m file:

#import "SortByTableViewController.h"

@interface SortByTableViewController () <UITableViewDelegate>

@end

@implementation SortByTableViewController

@synthesize selectedSection = _selectedSection;
@synthesize receivedOption = _receivedOption;
@synthesize delegate = _delegate;

...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ItemCharacteristic *itemCharacteristic = [self.fetchedResultsController objectAtIndexPath:indexPath];
    [self.delegate sortByTableViewController:self returnedOption:itemCharacteristic];
    [self dismissViewControllerAnimated:YES completion:nil]; //THIS DOESN'T WORK
    [self.navigationController popViewControllerAnimated:YES]; //THIS DOESN'T WORK EITHER
}

@end

Thanks for any help or guidance.


Solution

  • I found the answer. I had to add the following property to my main screen:

    @property (nonatomic, strong) UIPopoverController *sortByPopoverController;
    

    Then, when launching the popover, I included this:

    UIStoryboardPopoverSegue *popoverSegue = (UIStoryboardPopoverSegue *)segue;
    self.sortByPopoverController = popoverSegue.popoverController;
    

    Including that code allowed me to properly dismiss the popover when the delegate called back:

    [self.sortByPopoverController dismissPopoverAnimated:YES];