13

我正在使用为 iPhone 创建弹出窗口的F​​PPopover类。我遵循了自述文件中的确切步骤,但我没有使用 xib 文件中的 UIbutton,而是使用以编程方式创建的 UIBarButtonItem。但是,我收到以下错误:

由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[UIBarButtonItem superview]:无法识别的选择器发送到实例 0x6a3e420”

我复制粘贴了与自述文件中相同的代码,但我只是将 (UIButton*)okButton 更改为 (id)sender (id 这里是 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];
}

我在想也许它与不是 UIButton 的 UIBarButtonItem 有关?或者是别的什么?我尝试将 UIBarButtonItem 转换为 UIButton 但仍然给了我同样的错误。有什么解决办法吗?

以防万一:这是我以编程方式创建导航栏和栏按钮的方式:

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

4 回答 4

24

presentPopoverFromView 仅接受 UIView 子类。UIBarButtonItem 不是 UIView 的子类,因此您需要找到与该按钮项相关的视图。这是我与 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];

这应该工作!让我知道!

于 2012-08-23T11:27:48.577 回答
2

那是 b/c UIBarButtonItem继承自UIBarItemand NSObject。只有继承自的 UI 元素UIView具有superview属性。

于 2012-08-18T10:21:40.683 回答
1

我遇到了同样的错误。解决方案是以编程方式创建一个 UIButton(在 UIBar 按钮项的同一位置 - 设置适当的坐标),然后从 UIButton 呈现弹出框。然后隐藏 UIButton。

这段代码对我有用:

-(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
}

希望这可以帮助。如果您有任何问题,请告诉我。

于 2012-08-22T06:18:23.147 回答
0

大多数应用程序显示来自 BarButtonItem 的弹出窗口。FPPopOverController 不支持这个。所以,在我看来你应该使用:WYPopOverController

于 2014-10-31T11:39:55.620 回答