Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone: Error when using the FPPopover class when using it with a UIBarButtonItem

I am using the FPPopover class which creates popups for iPhones. I followed the exact steps that are in the readme file but instead of using a UIbutton from a xib file, I am using a UIBarButtonItem created programmatically. But, I get the following error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIBarButtonItem superview]: unrecognized selector sent to instance 0x6a3e420'

I copy pasted the same code as in the readme file but I just changed the (UIButton*)okButton with (id)sender (id here is UIBarButtonItem*)

-(void)popover:(id)sender
{
    //the view controller you want to present as popover
    TestClass *controller = [[TestClass alloc] init];
    //our popover
    FPPopoverController *popover = [[FPPopoverController alloc] initWithViewController:controller];
    //the popover will be presented from the okButton view
    [popover presentPopoverFromView:sender];
    //release
    [controller release];
}

I was thinking maybe it has to do with the UIBarButtonItem which is not a UIButton? Or is it anything else? I tried converting the UIBarButtonItem into UIButton but still gave me the same error. Any solutions to it?

One more note just in case: This is how I programmatically created the navigation bar along with the bar button:

UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(320, 0, 320, 44)];
UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:@"By Clubs"];
[navBar pushNavigationItem:navItem animated:NO];
UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:@"Filter"
                                               style:UIBarButtonItemStyleBordered
                                              target:self
                                              action:@selector(popover:)];


navItem.rightBarButtonItem = editButton;
like image 408
Guy Daher Avatar asked Aug 18 '12 09:08

Guy Daher


1 Answers

the presentPopoverFromView is only accepting a UIView subclass. UIBarButtonItem is not a subclass of UIView, so you need to find the view related to that button item. This is the solution I'm using with FPPopoverController

    UIBarButtonItem *buttonItem = sender;
    UIView* btnView = [buttonItem valueForKey:@"view"];
    //On these cases is better to specify the arrow direction
    [popover setArrowDirection:FPPopoverArrowDirectionUp];
    [popover presentPopoverFromView:btnView];

This should work! Let me know!

like image 98
Alvise Susmel Avatar answered Oct 08 '22 07:10

Alvise Susmel