2

Here's my code

-(IBAction)showMenu:(id)sender
{   
    Demo   *mainMenuTableView = [[Demo alloc] init];
    UIPopoverController *pop = [[UIPopoverController alloc]initWithContentViewController:mainMenuTableView];
    [pop setDelegate:self];

}

Demo is my xib that contains a tableview controller stuff. This "Demo" works just fine as a fullscreen view.

I'm trying to create a popover with this view, but I've tried what I think is every other solutions on stackoverflow, but I still cannot determine how to create and call the popover...

I'm sure I'm like a line of code or two away... I hope. Any help would be appreciated!

Thx!

Calderon
  • 97
  • 3
  • 9

2 Answers2

12

After you create the popover controller, you have to tell it to present the popover. You can use either presentPopoverFromRect:inView:permittedArrowDirections:animated: or presentPopoverFromBarButtonItem:permittedArrowDirections:animated:. For example, I assume that you have connected showMenu: as the action of a UIButton. So you can add this at the end of showMenu::

UIButton *button = (UIButton *)sender;
[pop presentPopoverFromRect:button.bounds
    inView:button
    permittedArrowDirections:UIPopoverArrowDirectionAny
    animated:YES];

You also need to put a reference to the popover controller in an instance variable or property. Otherwise the popover controller will be deallocated when showMenu: returns, which will cause a crash. Thanks to Floydian for pointing this out.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • showMenu is in fact connected to the action of UIButton. I added the code as you posted it, and the app still crashes when I press the button. I also attempted it as this '[pop presentPopoverFromRect:CGRectMake(0, 0, 100, 100) inView:button permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES]; ' but it also still crashes... – Calderon Jan 21 '12 at 07:04
  • Without seeing the stack trace, we can't help you diagnose the problem. Edit your question and paste in the stack trace. – rob mayoff Jan 21 '12 at 07:08
  • 2012-01-21 01:15:01.049 App[21913:15203] *** Terminating app due to uncaught exception 'NSGenericException', reason: '-[UIPopoverController dealloc] reached while popover is still visible.' – Calderon Jan 21 '12 at 07:16
  • That is not a stack trace. You need to [set an exception breakpoint](http://stackoverflow.com/questions/4961770/run-stop-on-objective-c-exception-in-xcode-4). Then when you get the exception, you need to copy the stack trace, **edit your question**, and paste the trace into your question. Do not try to put the stack trace into a comment. – rob mayoff Jan 21 '12 at 07:21
  • 1
    You need to have a UIPopoverController property on your .h file (instance var): UIPopoverController *pop; and use it when presenting the popover... pop = [[UIPopoverController alloc] initWithContentViewController:mainMenuTableView]; – Floydian Oct 17 '12 at 16:48
0

You need to retain the "pop" variable! Just set a UIPopoverController "POV" as your property and use below code in your IBAction.

self.POV = [[UIPopoverController alloc]initWithContentViewController:mainMenuTableView];
[self.POV presentPopoverFromRect:button.bounds
inView:button
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
realsnake
  • 444
  • 2
  • 9
  • 18