2011-11-02 35 views
1

我有一個滾動視圖,並添加了一個帶有圖像標籤和按鈕的視圖到滾動視圖。我將操作添加到按鈕,當我運行它時,一切看起來都很好,但是當我點擊某個按鈕時,應用程序與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的解除分配對象(zombie)。 

+0

告訴我這個類的名字,以及這個類的對象是在哪裏創建的? –

+0

Inder此方法在UIViewController中,類名稱爲LevelPackViewController。這是在用戶按下按鈕(開始按鈕)時從另一個視圖創建的。 – Chevol

+0

如果我將組件添加到視圖而不是UIScrollView,它可以正常工作,但我顯然不能滾動通過關卡包。然而,點擊它們不會返回錯誤,因此它使我認爲UIScrolView正在被釋放,所以是對按鈕和目標idk的引用。 – Chevol

回答

1

什麼對象是displayPacks的一種方法?是否在displayPacks返回後保留該對象?請記住,像UIButton這樣的控件不會而不是保留其目標,因此您需要其他操作。

+0

displayPacks是UIViewController的一種方法。我正在使用ARC,並假設它在displayPacks返回後被保留。我沒有意識到UIButton不會保留它們的目標,我過去做過類似的事情,就是沒有使用ARC。我所要做的只是將圖像,標籤和按鈕添加到滾動視圖中,並讓它響應觸摸事件以查看用戶選擇了哪一個。 – Chevol

1

我還沒有完全弄明白,但看起來像bottons由於某種原因不保留。我所做的防止這種問題是使用類屬性NSMutableArray * my_buttons來保存臨時按鈕:

UIButton * aButton = [[UIButton alloc] init ...]; ... [my_buttons addObject:aButton];

當然,如果只有一個按鈕,你可以使它成爲一個類屬性。無論如何避免使用它作爲局部變量。但是,這只是一些解決方法,我不知道你如何在ARC環境中「保留」本地變量。