1

저는 scrollview가 있으며 scrollview에 이미지 레이블과 버튼이있는보기를 추가하고 있습니다. 버튼에 액션을 추가하면 모든 것을 실행하면 잘 보이지만 버튼을 클릭하면 앱이 EXC_BAD_ACCESS와 충돌합니다. ARC를 사용하고있어 문제를 일으키는 지 확실하지 않지만 그렇게해서는 안됩니다. 그것은 마치 뭔가가 기억에서 풀린 것처럼 내 행동을 찾지 못하는 것 같습니다. 나는 그 범인 인에 부착하고 그 이유는 내가 정말 여기에 뭔가를 놓친 거지UIButton 조치 EXC_BAD_ACCESS ARC

-(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); 
} 

, 내가 ARC와 함께 전에 이런 짓을하지만하지 않은 : 여기 내 코드입니다.

NSZombie 출력 상태 : Objective-C 메시지가 주소 : 0x6b8d530의 할당 해제 된 개체 (좀비)로 전송되었습니다. 

+0

나에게 클래스의 이름과이 클래스의 객체가 생성되는 위치를 알려주십시오. –

+0

이 메소드는 UIViewController 내부에 있으며 클래스 이름은 LevelPackViewController입니다. 이것은 사용자가 버튼 (시작 버튼)을 누를 때 다른 뷰에서 생성됩니다. – Chevol

+0

UIScrollView 대신보기에 구성 요소를 추가하면 레벨 팩을 스크롤 할 수 없다는 것 외에는 잘 작동합니다. UIScrolView가 릴리스되고 버튼과 대상, idk에 대한 참조가 있다고 생각하게하므로 오류를 반환하지 않습니다. – Chevol

답변

1

어떤 개체가 displayPacks의 방법입니까? displayPacks가 반환 한 후에 해당 객체가 유지됩니까? UIButton과 같은 컨트롤은이 아닌 이 목표를 유지하므로 다른 작업이 필요합니다.

+0

displayPacks는 UIViewController의 한 메서드입니다. ARC를 사용하고 있는데 displayPacks가 반환 된 후에 ARC가 유지되고 있다고 가정합니다. 나는 UIButton이 목표를 유지하지 않는다는 것을 알지 못했지만 과거에는 ARC를 사용하지 않았던 비슷한 일을했다. 제가하려는 것은 scrollview에 이미지, 라벨 및 버튼을 추가하고 터치 이벤트에 응답하여 사용자가 선택한 것을 확인하는 것입니다. – Chevol

1

전적으로 알아 내지 못했지만 어떤 이유로 든 보턴이 유지되지 않는 것처럼 보입니다. 이러한 문제를 방지하기 위해 NSMutableArray * my_buttons 클래스 속성을 사용하여 임시 단추를 유지합니다. ... [my_buttons addObject : aButton];

물론 버튼이 1 개인 경우 클래스 속성으로 만들 수 있습니다. 어쨌든 로컬 변수로 사용하지 마십시오. 그러나 다시 이것은 단지 몇 가지 해결 방법 일 뿐이며 ARC 환경에서 로컬 변수를 "유지"하는 방법을 알지 못합니다.