Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIPopOverController + UITableView - Hide popover when cell is selected

In my Popover controller, i'm having a table view. On selection of a cell, I want to hide the pop over. How can I achieve it.

like image 725
Satyam Avatar asked Oct 28 '11 18:10

Satyam


4 Answers

In Header file of Root view controller:

@property (strong, nonatomic) UIStoryboardPopoverSegue* popSegue;

In the implementation file:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if( [[segue identifier] isEqualToString:@"popover"] )
    {
        NSLog(@"%@",[segue destinationViewController]);
        self.popSegue = (UIStoryboardPopoverSegue*)segue;

        [[segue destinationViewController] setDelegate:self];
    }
}

When ever you want to hide the pop over:

    if ([self.popSegue.popoverController isPopoverVisible]) 
    {
        [self.popSegue.popoverController dismissPopoverAnimated:YES];        
    }

In the table view, add a delegate and implement the delegate in root view controller. When the delegate method is called, use above code to dismiss the pop over.

like image 115
Satyam Avatar answered Nov 09 '22 17:11

Satyam


Allow me to suggest a slightly different solution, which consists in passing the popover controller reference instead of the segue reference.

In the implementation file of the source view controller:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue destinationViewController] isKindOfClass:[MyDestViewController class]]) {
        MyDestViewController* viewController = (MyDestViewController*)[segue destinationViewController];
        UIStoryboardPopoverSegue* popoverSegue = (UIStoryboardPopoverSegue*)segue;
        [viewController setPopoverController:[popoverSegue popoverController]];
    }
}

In the header file of the destination view controller:

@property (weak, nonatomic) UIPopoverController* popoverController;

In the implementation file of the destination view controller:

@synthesize popoverController;

Same file, whenever you want to dismiss the popover:

[popoverController dismissPopoverAnimated:YES];
like image 43
Giorgio Barchiesi Avatar answered Nov 09 '22 16:11

Giorgio Barchiesi


The apple docs recommend the following:

Dismissing a popover programmatically requires a pointer to the popover controller. The only way to get such a pointer is to store it yourself, typically in the content view controller. This ensures that the content view controller is able to dismiss the popover in response to appropriate user actions.

http://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/ViewControllerCatalog/Chapters/Popovers.html

like image 1
Alfie Hanssen Avatar answered Nov 09 '22 15:11

Alfie Hanssen


in didSelectRowAtIndexPath try this code

[viewController.popoverController dismissPopoverAnimated:YES];
like image 1
iOS developer Avatar answered Nov 09 '22 17:11

iOS developer