从我上一个问题开始,我修改了我的代码以从plist中获取图片。我之前的问题在这个链接中(Fetching Selected Image From An Array to be displayed in another view)
我的代码是:
- (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:
中提取的图像..任何人都可以帮我解决这个问题?在此先感谢..
答案 0 :(得分:1)
在YurSecondViewController.h
UIImage *img;
@property(nonatomic,retain)UIImage *img;
确保您property
和synthesize
img 。
在FirstViewController
所在地image
: -
YurSecondViewController *View=[[[YurSecondViewController alloc]init];
View.img=[UIImage imageNamed:@"Default.png"];
[self.navigationController pushViewController:View animated:YES];
在您必须显示图像的YurSecondViewController.m中: -
UIImageView *imgView=[[UIImageView alloc]initWithFrame:CGRectMake(50, 50, 100, 100)];
[imgView setImage:self.img];
[self.view addSubview:imgView];