gpt4 book ai didi

iphone - UIPickerView EXC 访问错误?

转载 作者:行者123 更新时间:2023-12-03 19:42:52 26 4
gpt4 key购买 nike

我不断收到错误访问错误,我认为这与我的 UIPickerView 有关,因为这是应用程序崩溃的时候。一切正常,直到我从 UIPickerView 中做出第九个选择。每次应用程序在第 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];
}

最佳答案

您没有保留 UIImage,因此它们会自动释放。每次 imageNamed 调用后,您都需要保留,即

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

或者,如果您已将它们声明为属性(即 @property (nonatomic, keep) 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当我写答案的后半部分时,我也有同样的清理代码的想法:)

关于iphone - UIPickerView EXC 访问错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2426651/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com