ios - objc_msgSend EXC_BAD_ACCESS 使用关闭按钮时

标签 ios objective-c xcode uiview uibutton

在我的项目中,我在右上角添加了一个关闭按钮,如下所示:

int closeBtnOffset = 10;
UIImage* closeBtnImg = [UIImage imageNamed:@"popupCloseBtn.png"];
UIButton* closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[closeBtn setImage:closeBtnImg forState:UIControlStateNormal];
[closeBtn setFrame:CGRectMake( background.frame.origin.x + background.frame.size.width - closeBtnImg.size.width - closeBtnOffset, 
                               background.frame.origin.y ,
                               closeBtnImg.size.width + closeBtnOffset, 
                               closeBtnImg.size.height + closeBtnOffset)];
[closeBtn addTarget:self action:@selector(closePopupWindow) forControlEvents:UIControlEventTouchUpInside];
[bigPanelView addSubview: closeBtn];

closePopupWindw 方法如下所示:

-(void)closePopupWindow
{
    //remove the shade
    [[bigPanelView viewWithTag: kShadeViewTag] removeFromSuperview];
    [self performSelector:@selector(closePopupWindowAnimate) withObject:nil afterDelay:0.1];

}

构建成功,但当我单击 closeBtn 按钮时,程序关闭并显示以下消息:http://i45.tinypic.com/ddndsl.png

我认为代码没有任何问题,因为我从另一个项目复制了它并且在那里运行良好,但在另一个项目中他们没有使用 ARC,我不确定这是否是问题所在。

编辑:

-(void)closePopupWindowAnimate
{

    //faux view
    __block UIView* fauxView = [[UIView alloc] initWithFrame: CGRectMake(10, 10, 200, 200)];
    [bgView addSubview: fauxView];

    //run the animation
    UIViewAnimationOptions options = UIViewAnimationOptionTransitionFlipFromLeft |
    UIViewAnimationOptionAllowUserInteraction    |
    UIViewAnimationOptionBeginFromCurrentState;

    //hold to the bigPanelView, because it'll be removed during the animation

    [UIView transitionFromView:bigPanelView toView:fauxView duration:0.5 options:options completion:^(BOOL finished) {

        //when popup is closed, remove all the views
        for (UIView* child in bigPanelView.subviews) {
            [child removeFromSuperview];
        }
        for (UIView* child in bgView.subviews) {
            [child removeFromSuperview];
        }

        [bgView removeFromSuperview];

    }];
}

最佳答案

您正在访问已经释放的对象,使用属性总是个好主意,并将属性类型设置为 strong(使用 ARC)以便它们可以保留只要您的 View 处于事件状态,就放在内存中。

将您的 UIButton 声明为类属性,它将解决您的问题。您还应该看到您的按钮已添加到 bigPanelView 上,并且您在调用方法 closePopupWindowAnimate

之前删除了此 View

关于ios - objc_msgSend EXC_BAD_ACCESS 使用关闭按钮时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15335789/

相关文章:

xcode - Xcode:如何将文件添加到文件夹引用

ios - Firebase 使用电子邮件和密码创建用户给我字符串必须解包错误

iphone - 清除 UIImage 上的绘图?

ios - 带有 Woocommerce 的 FlexSlider : prevent vertical swipe touch on horizontal sliders

ios - 解析数据未出现在 iOS 9 Beta 4 中

ios - 将自定义 UIView 裁剪为三角形

objective-c - 有没有办法在 iOS 上获取相机流的亮度级别?

ios - UiCollectionView 单元格选择错误

ios - NSDateFormatter 崩溃

ios - 在 iOS 设备 XCODE8 上没有进行调试