【问题标题】:Popover crashing on UIBarButtonItem action in iOS弹出窗口在 iOS 中的 UIBarButtonItem 操作上崩溃
【发布时间】:2015-02-05 10:34:22
【问题描述】:

创建了一个自定义 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的实例:

谁能帮忙?

【问题讨论】:

  • 你在你的项目中使用了这一行 self.navigationItem.leftBarButtonItem = barBtnFavorites;
  • 在您的情况下,发件人是 UIButton 而不是 UIBarButtonItem,它是 actionButtonFavorite: is expecting 类型
  • 不!发件人是 UIBarButtonItem 的实例,请参阅我编辑的帖子
  • 两个选项:要么你要求UIButton 给你一个view,它不理解,要么你要求其他的view 和那个东西else 已被释放,其内存重新用于UIButton。如果您在构建方案中打开僵尸,它将有助于确定您需要解决的问题。如果错误保持不变,则为#1;如果错误提到了一个不同的类,它是#2。 (在任何一种情况下,符号化的堆栈跟踪都可能会有所帮助。)

标签: ios uibarbuttonitem uipopovercontroller


【解决方案1】:

当然 sender 是一个 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:)];

【讨论】:

  • 嗨,我想要自定义按钮而不是系统按钮。它与自定义按钮崩溃。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 2020-12-23
  • 1970-01-01
  • 2014-06-22
  • 2011-09-30
  • 1970-01-01
相关资源
最近更新 更多