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

后端 未结 4 1092
野的像风
野的像风 2021-01-11 10:19

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

相关标签:
4条回答
  • 2021-01-11 10:53

    I was getting the same error. Solution is to create a UIButton Programatically (at the same place of UIBar button item - set the appropriate coordinates) and then present popover from UIButton. Then hide the UIButton.

    This code worked for me:

    -(void)testMethod {
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    
    // set action as NULL if you dont need any method/functionality to call
    
    [button addTarget:self action:@selector(aMethod)
     forControlEvents:UIControlEventTouchDown];
    
    
    [button setTitle:@"Show View" forState:UIControlStateNormal];
    button.frame = CGRectMake(130, -40, 160.0, 40.0);
    [self.view addSubview:button];
    
    AlertsViewController *controller = [[AlertsViewController alloc] init]; 
    
    //our popover
    FPPopoverController *popover = [[FPPopoverController alloc] initWithViewController:controller]; 
    
    //the popover will be presented from the Button view 
    [popover presentPopoverFromView:button]; 
    
    //hide the button
    button.hidden = YES;
    
    }
    
    -(void)aMethod {
    // Write any functionality if you need
    }
    

    Hope this helps. Let me know if you get any issue.

    0 讨论(0)
  • 2021-01-11 11:01

    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!

    0 讨论(0)
  • 2021-01-11 11:14

    Mostly applications display popover from BarButtonItem. FPPopOverController doesn't support this. So, in my opinion you should use: WYPopOverController

    0 讨论(0)
  • 2021-01-11 11:16

    That's b/c UIBarButtonItem inherits from UIBarItem and NSObject. Only UI elements that inherit from UIView have superview properties.

    0 讨论(0)
提交回复
热议问题