Calderon
Calderon

Reputation: 97

PopOver from UIButton

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!

Upvotes: 2

Views: 8453

Answers (2)

rob mayoff
rob mayoff

Reputation: 386018

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.

Upvotes: 12

realsnake
realsnake

Reputation: 444

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];

Upvotes: 0

Related Questions