私の最後の質問から、plistから画像を取得するようにコードを変更しました..私の以前の質問はこのリンクにあります(配列から選択した画像を取得して別のビューに表示する)
私のコードは次のとおりです。
- (void)viewDidLoad
{
[super viewDidLoad];
NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ImageList" ofType:@"plist"] ];
self.arrayImages = [dictionary objectForKey:@"Images"];
selectedTag = 0;
images = [[NSMutableArray alloc]init];
lists = [[NSMutableArray alloc]init];
imageScroll.delegate = self;
imageScroll.scrollEnabled = YES;
int scrollWidth = 768;
imageScroll.contentSize = CGSizeMake(scrollWidth,605);
int xOffset = 0;
int total = self.arrayImages.count;
for(index=0; index < total; index++)
{
dict = [self.arrayImages objectAtIndex:index];
UIButton *img = [UIButton buttonWithType:UIButtonTypeCustom];
UILabel *lab = [[UILabel alloc] init];
img.tag = index;
selectedTag = img.tag;
img.bounds = CGRectMake(10, 10, 50, 50);
img.frame = CGRectMake(50+xOffset, 120, 270, 350);
lab.bounds = CGRectMake(20, 20, 150, 150);
lab.frame = CGRectMake(160+xOffset, 500, 90, 50);
lab.font = [UIFont systemFontOfSize:24];
[img setImage:[UIImage imageNamed:[dict objectForKey:@"imagesName"]] forState:UIControlStateNormal];
lab.text = [NSString stringWithFormat:[dict objectForKey:@"listName"]];
[images insertObject:img atIndex:index];
[lists insertObject:lab atIndex:index];
imageScroll.contentSize = CGSizeMake(scrollWidth+xOffset,510);
[imageScroll addSubview:[images objectAtIndex:index]];
[imageScroll addSubview:[lists objectAtIndex:index]];
xOffset += 370;
[img addTarget:self action:@selector(newView:) forControlEvents:UIControlEventTouchUpInside];
selectedTag = img.tag;
}
}
-(void)newView:(id)sender{
NSLog(@"%@",sender);
int newIndex;
newIndex = [sender tag];
NSLog(@"Selected Tag :%d",newIndex);
CGRect rec = CGRectMake(0, 0, 250, 250);
[self setView:[[[UIView alloc] initWithFrame:rec] autorelease]];
[[self view] setBackgroundColor:[UIColor whiteColor]];
UIImage *img = [UIImage imageNamed:[[dict objectForKey:@"imagesName"]objectAtIndex:newIndex]];
UIImageView *im = [[UIImageView alloc] initWithImage:img];
UILabel *lab1 = [[UILabel alloc] init];
lab1.text = [NSString stringWithFormat:[[dict objectForKey:@"listName"] objectAtIndex:newIndex]];
lab1.bounds = CGRectMake(20, 20, 150, 150);
lab1.frame = CGRectMake(360, 600, 90, 50);
lab1.font = [UIFont systemFontOfSize:24];
im.bounds = CGRectMake(10, 10, 50, 50);
im.frame = CGRectMake(200, 120, 370, 450);
[[self view] addSubview:im];
[[self view] addSubview:lab1];
}
私のPlist出力:
Images = (
{
Contents = "Sea While Tsunami";
imagesName = "sea1.jpg";
listName = Sea1;
},
{
Contents = "Sea While Tsunami";
imagesName = "sea2.jpg";
listName = Sea2;
},
{
Contents = "Sea While Tsunami";
imagesName = "sea3.jpg";
listName = Sea3;
},
{
Contents = "Sea While Tsunami";
imagesName = "sea4.jpg";
listName = Sea4;
},
{
contents = "Sea While Tsunami";
imagesName = "sea5.png";
listName = Sea5;
},
),
次のようにエラーが発生します:
<UIButton: 0x714f3a0; frame = (420 120; 270 350); opaque = NO; tag = 1; layer = <CALayer: 0x4f460>>
2013-02-12 12:58:46.973 image[2546:11303] Selected Tag :1
2013-02-12 12:58:46.975 image[2546:11303] -[__NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x714bd60
2013-02-12 12:58:46.976 image[2546:11303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x714bd'
***First throw call stack:
(0x8f012 0xcce7e 0x1a4bd 0x7ebbc 0x7e94e 0x3584 0xe0705 0xe97e3 0xe9668 0x1465c 0x26a2 0x25d5)
libc++abi.dylib: terminate called throwing an exception
plistは正常に動作します..最初のビューではうまく表示されます..しかし、選択した画像を別のビューにフェッチすると、このエラーが発生します..フェッチした画像を初期化するのを間違えていますnewView:
..誰かがこの問題を解決するのを手伝ってくれますか?? 前もって感謝します..