UIPickerView EXC BAD ACCESS?

时间:2010-03-11 16:20:18

标签: iphone uipickerview exc-bad-access

我一直遇到一个错误的访问错误,我认为它与我的UIPickerView有关,因为这是应用程序崩溃的时候。一切正常,直到我从UIPickerView做出第9个选择。应用程序每次都在第9个选项崩溃。有什么想法吗?

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

2 个答案:

答案 0 :(得分:4)

你没有保留你的UIImages,所以他们被自动释放。每次进行图像命名调用后,都需要保留,即

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在编写我的答案的后半部分时,对于清理代码有同样的想法:)

答案 1 :(得分: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];