2010-03-11 82 views
3

我不斷收到錯誤的訪問錯誤,我認爲這與我的UIPickerView有關,因爲這是應用程序崩潰的時候。一切工作正常,直到我從UIPickerView做出第9個選擇。每次該應用程序崩潰的第九個選擇。有任何想法嗎?UIPickerView EXC BAD ACCESS?

- (void)viewDidLoad { 
     [super viewDidLoad]; 



    list = [[NSMutableArray alloc] init]; 
    [list addObject:@"Anvil"]; 
    [list addObject:@"Apple"]; 
    [list addObject:@"Arrow"]; 
    [list addObject:@"Baby"]; 
    [list addObject:@"Basketball"]; 
    [list addObject:@"Beehive"]; 
    [list addObject:@"Blimp"]; 
    [list addObject:@"Bomb"]; 
    [list addObject:@"Bungee Jumper"]; 
    [list addObject:@"Cactus"]; 
    [list addObject:@"Cake"]; 
    [list addObject:@"Car"]; 
    [list addObject:@"Caterpillar"]; 
    [list addObject:@"Couch"]; 
    [list addObject:@"Dennis"]; 

     anvil = [UIImage imageNamed:@"anvil.png"]; 
    apple = [UIImage imageNamed:@"apple.png"]; 
    arrow = [UIImage imageNamed:@"arrow.png"]; 
    baby = [UIImage imageNamed:@"baby.png"]; 
    basketball = [UIImage imageNamed:@"basketball.png"]; 
    beehive = [UIImage imageNamed:@"beehive.png"]; 
    blimp = [UIImage imageNamed:@"blimp.png"]; 
    bomb = [UIImage imageNamed:@"bomb.png"]; 
    bungeejumper = [UIImage imageNamed:@"bungeejumper.png"]; 
    cactus = [UIImage imageNamed:@"cactus.png"]; 
    cake = [UIImage imageNamed:@"cake.png"]; 
    car = [UIImage imageNamed:@"car.png"]; 
    caterpillar = [UIImage imageNamed:@"caterpillar.png"]; 
    couch = [UIImage imageNamed:@"couch.png"]; 
    dennis = [UIImage imageNamed:@"dennis.png"]; 
    } 

    -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView{ 
    return 1; 
    } 

    -(NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component{ 

    return [list count]; 
    } 

    -(NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ 

    return [list objectAtIndex:row]; 
    } 

    - (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { 

    if([[list objectAtIndex:row] isEqual:@"Anvil"]) { 
     [object setImage:anvil]; 
    } 
    else if([[list objectAtIndex:row] isEqual:@"Apple"]) { 
     [object setImage:apple]; 
    } 
    else if([[list objectAtIndex:row] isEqual:@"Arrow"]) { 
     [object setImage:arrow]; 
    } 
    else if([[list objectAtIndex:row] isEqual:@"Baby"]) { 
     [object setImage:baby]; 
    } 
    else if([[list objectAtIndex:row] isEqual:@"Basketball"]) { 
     [object setImage:basketball]; 
    } 
    else if([[list objectAtIndex:row] isEqual:@"Beehive"]) { 
     [object setImage:beehive]; 
    } 
    else if([[list objectAtIndex:row] isEqual:@"Blimp"]) { 
     [object setImage:blimp]; 
    } 
    else if([[list objectAtIndex:row] isEqual:@"Bomb"]) { 
     [object setImage:bomb]; 
    } 
    else if([[list objectAtIndex:row] isEqual:@"Bungee Jumper"]) { 
     [object setImage:bungeejumper]; 
    } 
    else if([[list objectAtIndex:row] isEqual:@"Cactus"]) { 
     [object setImage:cactus]; 
    } 
    else if([[list objectAtIndex:row] isEqual:@"Cake"]) { 
     [object setImage:cake]; 
    } 
    else if([[list objectAtIndex:row] isEqual:@"Car"]) { 
     [object setImage:car]; 
    } 
    else if([[list objectAtIndex:row] isEqual:@"Caterpillar"]) { 
     [object setImage:caterpillar]; 
    } 
    else if([[list objectAtIndex:row] isEqual:@"Couch"]) { 
     [object setImage:couch]; 
    } 
    else if([[list objectAtIndex:row] isEqual:@"Dennis"]) { 
     [object setImage:dennis]; 
    } 
    } 

- (void)dealloc { 
[list release]; 
    [super dealloc]; 
[animation release]; 
} 
+0

這不回答你的問題,但你一定要能夠把[超級的dealloc];作爲dealloc方法的最後一行 - 否則當對象被釋放時你可能會崩潰! – deanWombourne 2010-03-11 16:40:47

+0

謝謝,沒有注意到。 – NextRev 2010-03-11 16:55:48

回答

4

你沒有保留你的UIImages,所以他們正在自動發佈。每imageNamed通話結束後,你需要保留即

baby = [[UIImage imageNamed:@"baby.png"] retain]; 

,或者,如果你已經聲明它們爲屬性(即@property (nonatomic, retain) UIImage *baby;),你可以這樣做:

self.baby = [UIImage imageNamed:@"baby.png"]; 

這是更正確的方法做到這一點。


然而,處理所有這些代碼的一個更好的辦法可能是使用圖像,而不是檢查每一次名字的數組。即

imageArray = [NSArray alloc] initWithObjects: 
       [UIImage imageNamed:@"Anvil.png"], 
       [UIImage imageNamed:@"Apple.png"], 
       [UIImage imageNamed:@"Arrow.png"], 
       nil]; 

,然後,一個項目被選定時,

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { 
    [object setImage:[imagearray objectAtIndex:row]]; 
} 

這是一個小吸塵器;)

編輯:Douglas已清理的代碼而予了同樣的想法正在寫下我的答案的第二部分:)

+0

救生員!如果我保留每個圖像,他們需要釋放dealloc權利? – NextRev 2010-03-11 16:49:53

+0

好點 - 是的,他們絕對需要釋放! – deanWombourne 2010-03-11 16:55:51

0

第九個對象是唯一一個在名稱中有空格的對象嗎?我不明白這會如何破壞它,尋找它與其他人之間的差異。該圖像不是被稱爲「bungee jumper.png」而不是「bungeejumper.png」是嗎?

旁白:而不是字符串列表,你可以有圖片標題對EG的列表,

// ... snip ... 
[self addPairWithTitle:@"Anvil" image:@"anvil.png"]; 
[self addPairWithTitle:@"Apple" image:@"apple.png"]; 

- (void) addPairWithTitle .... 
{ 
    // you'll need to define a MyNewPair object which retains the image and title 
    [list addObject:[[MyNewPair alloc] initWithTitle:title andImage:[UIImage imageNamed:imageName]]; 
} 

// ... snip ... 

... titleForRow... 
return [[list objectAtIndex:row] title]; 

...didSelectRow... 
[object setImage:[[list objectAtIndex:row]] image];