在我的项目中,我在右上角添加了一个关闭按钮,如下所示:
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
关于ios - objc_msgSend EXC_BAD_ACCESS 使用关闭按钮时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15335789/