Objective c UIButton动作EXC_坏_进入弧
我有一个scrollview,正在向scrollview添加带有图像标签和按钮的视图。我正在向按钮添加操作,当我运行它时,一切看起来都很好,但当我单击按钮时,应用程序会崩溃,无法访问。我使用ARC,所以不确定这是否会导致问题,但它不应该。它似乎找不到我的动作,好像有什么东西从记忆中释放了出来。这是我的密码:Objective c UIButton动作EXC_坏_进入弧,objective-c,cocoa-touch,uibutton,automatic-ref-counting,Objective C,Cocoa Touch,Uibutton,Automatic Ref Counting,我有一个scrollview,正在向scrollview添加带有图像标签和按钮的视图。我正在向按钮添加操作,当我运行它时,一切看起来都很好,但当我单击按钮时,应用程序会崩溃,无法访问。我使用ARC,所以不确定这是否会导致问题,但它不应该。它似乎找不到我的动作,好像有什么东西从记忆中释放了出来。这是我的密码: -(void)lvlSelected:(id)sender{ NSLog(@"Selected level pack"); } /* DISPLPAYPACKS This wi
-(void)lvlSelected:(id)sender{
NSLog(@"Selected level pack");
}
/*
DISPLPAYPACKS
This will take the loaded level packs and display them in the scroll view for selection
*/
-(void)displayPacks{
//Iterate thru the installed level packs and load some tiles they can select
for (int i = 0; i < installedLevelPacks.count; i++){
levelPack *curPack = [installedLevelPacks objectAtIndex:i];
CGFloat x = i * 110;
//Add the view to contain the rest of our elements
UIView *tileView = [[UIView alloc] initWithFrame:CGRectMake(x, 0, 100, 125)];
//view.backgroundColor = [UIColor greenColor];
//Add a label to the bottom to hold the title
UILabel *titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(0, tileView.frame.origin.y+100, 100, 25)];
titleLbl.textAlignment=UITextAlignmentCenter;
titleLbl.font=[UIFont fontWithName:@"American Typewriter" size:12];
titleLbl.adjustsFontSizeToFitWidth=YES;
titleLbl.text=curPack.title;
//Add the preview image to the tile
UIImageView *previewImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:curPack.previewImg]];
previewImg.frame=CGRectMake(0, 0, 100, 100);
//Add the button over the tile
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
aButton.frame = CGRectMake(0, 0, 100, 125);
//Set the tag to the level ID so we can get the pack later
[aButton setTag:i];
[aButton setTitle:curPack.title forState:UIControlStateNormal];
[aButton addTarget:self action:@selector(lvlSelected:) forControlEvents:UIControlEventTouchUpInside];
[tileView addSubview:previewImg];
[tileView addSubview:titleLbl];
[tileView addSubview:aButton];
[lvlScroller addSubview:tileView];
}
//Set the total size for the scrollable content
lvlScroller.contentSize = CGSizeMake(installedLevelPacks.count*110, 125);
}
-(void)lvlSelected:(id)发送方{
NSLog(“选定级别包”);
}
/*
工资袋
这将获取加载的级别包,并在滚动视图中显示它们以供选择
*/
-(无效)显示包{
//遍历已安装的级别包并加载一些他们可以选择的磁贴
对于(int i=0;i
我真的错过了一些东西,我以前做过这件事,但不是用ARC,所以这就是为什么我被认为是罪魁祸首的原因
NSZombie输出状态:
已向地址为0x6b8d530的解除分配对象(僵尸)发送Objective-C消息。 什么对象是
displayPacks
方法?displayPacks
返回后是否保留该对象?请记住,像UIButton
do而不是这样的控件会保留它们的目标,因此您需要其他方法来执行此操作。我还没有完全弄清楚,但看起来由于某种原因,僵尸不会被保留。我所做的防止此类问题的工作是使用类属性NSMutableArray*my_buttons来保存临时按钮:
UIButton*aButton=[[UIButton alloc]init…];
...
[my_buttons addObject:aButton]
当然,如果只有一个按钮,您可以将其设置为类属性。无论如何,请避免将其用作局部变量。但同样,这只是一些解决方法,我真的不知道如何在ARC环境中“保留”局部变量。告诉我类的名称以及创建此类对象的位置???如果此方法位于UIViewController中,则类名为LevelPackViewController。当用户按下按钮(开始按钮)时,这是从另一个视图创建的。如果我将组件添加到视图中而不是UIScrollView,它可以正常工作,但我显然无法滚动查看级别包。但是,单击它们不会返回错误,因此它使我认为UIScrolView正在被释放,对按钮和目标的引用也是如此,idk.displayPacks是UIViewController的一种方法。我正在使用ARC,并假设它在displayPacks返回后被保留。我不知道UIButton没有保留他们的目标,我在过去做过类似的事情,只是没有使用ARC。我所要做的就是在scrollview中添加一个图像、标签和按钮,并让它响应触摸事件,以查看用户选择了哪一个。