Popover在iOS中的UIBarButtonItem操作上崩溃

时间:2015-02-05 10:34:23

标签: ios uibarbuttonitem uipopovercontroller

创建了自定义UIBarButtonItem:

UIButton *favButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0, 0.0, 42.0, 30.0)];
[favButton setImage:[UIImage imageNamed:@"iphone-navbar-icon-star-normal.png"] forState:UIControlStateNormal];
[favButton addTarget:self action:@selector(actionButtonFavorite:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barBtnFavorites = [[UIBarButtonItem alloc] initWithCustomView:favButton];

点击按钮点击我打开一个弹出窗口。

- (void)actionButtonFavorite:(UIBarButtonItem *)sender
{
    self.selectedButtonTag = sender.tag;
    favoriteOptionsVC.contentSizeForViewInPopover = CGSizeMake(favoriteOptionsVC.view.frame.size.width, (IS_iOS_VERSION_7?190.0:160.0));
    UINavigationController *favoritesNavVC = [[UINavigationController alloc] initWithRootViewController:favoriteOptionsVC];
    self.favoritesPopoverController = [[UIPopoverController alloc] initWithContentViewController:favoritesNavVC];
    favoriteOptionsVC.containingPopoverController = self.favoritesPopoverController;
    [self.favoritesPopoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}

但该应用程序崩溃说:

[UIButton view]: unrecognized selector sent to instance 0x7a7445e0

发件人是UIBarButtonItem的实例:

enter image description here

enter image description here

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

当然发送者是一个UIBarButtonItem,它在你的函数中隐式声明。真正的发送者(调用该方法的实例)是一个UIButton。 试试这个:

UIBarButtonItem *barBtnFavorites = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(actionButtonFavorite:)];

要使“自定义”UIBarButtonItem使用此:

UIBarButtonItem *barBtnFavorites = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iphone-navbar-icon-star-normal.png"]
                                                                   style:UIBarButtonItemStylePlain
                                                                  target:self
                                                                  action:@selector(actionButtonFavorite:)];