1

How do you dismiss a popover from within a navigation stack. I have a navigation controller as the root controller of the popover and 2 taes vies in the stack. So that the first table view pushes the second and the second should dismiss the popover. I could pass a reference from table to table of the popover though this seems wrong. What is the preferred way of dismissing a popover after navigated through different controllers?

Jonathan.
  • 53,997
  • 54
  • 186
  • 290

1 Answers1

6

In your appdelegate, add a new NSNotificationCenter observer:

[[NSNotificationCenter defaultCenter] addObserver:self 
selector:@selector(hidePopover)
name:@"hidePopover"
object:nil];

Once you have that setup, add a new method within the appdelegate like so:

-(void)hidePopover{
    [UIPopoverController dismissPopoverAnimated:YES];
}

This approach is great, because now you have things set up in such a way that you can close the popover from anywhere. You do this like so:

[[NSNotificationCenter defaultCenter] postNotificationName:@"hidePopover" 
object:nil];

Hope this solves your conundrum,

Zane

Aurum Aquila
  • 9,126
  • 4
  • 25
  • 24
  • But dismissPopover method is not a class method. So I'd need a reference to the popover in the app delegate?? Why can't it just work like modal view controllers? – Jonathan. Jan 24 '11 at 11:02
  • If you read the apple docs, they say to keep a reference to the popover controller as a property in your header, so that the view does not have to be created every time it is shown. You would replace UIPopOverController with the name of your object. Sorry, I should have been more clear about that. – Aurum Aquila Jan 24 '11 at 12:17
  • Is that in the UIPopoverController class reference? – Jonathan. Jan 24 '11 at 19:02
  • Yeah, just replace the UIPopoverController part with the name of your object reference (myPopover) – Aurum Aquila Jan 25 '11 at 08:07