0

从我的上一个问题中,我修改了我的代码以从 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:..有人可以帮我解决这个问题吗?提前致谢..

4

1 回答 1

1

声明它在YurSecondViewController.h

UIImage *img;
@property(nonatomic,retain)UIImage *img;

确保你 makepropertysynthesize 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];
于 2013-02-12T08:02:47.273 回答