0

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

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

    }];
}
4

2 回答 2

3

您正在访问已经发布的对象,使用属性并设置属性类型strong(使用ARC)总是一个好主意,这样只要您的视图处于活动状态,它们就可以保留在内存中的位置。

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

于 2013-03-11T10:26:29.393 回答
0

快速浏览一下,我怀疑问题可能是 closePopupWindow 中的 performSelector 调用:按钮由 bigPanelView 保留,但这在第一行中被释放。这可能意味着在调用 performSelector 之前释放“self”。

作为风格问题,避免使用视图标签:在某种父对象中为相关视图定义属性要好得多。这也可以更轻松地避免保留周期和早期版本,例如您在此处遇到的问题

于 2014-04-08T08:22:33.373 回答