Grey Code
Grey Code

Reputation: 326

Fetching Selected Image From plist to be displayed in another view

From my last question i've modified my code to fetch images from plist..My Previous question is in this link(Fetching Selected Image From An Array to be displayed in another view)

My Code is:

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

My Plist Output:

   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;
    },


 ),

I'm Getting error as:

<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 works fine..it displays well on first view..but on fetching the selected image to another view i'm getting this error..am i mistaking on initializing fetched image in newView:..can anybody help me in solving this problem?? Thanks In Advance..

Upvotes: 0

Views: 320

Answers (1)

iPatel
iPatel

Reputation: 47069

Declare it in YurSecondViewController.h

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

Make sure you make property and synthesize img.

In the FirstViewController where you have that image:-

YurSecondViewController *View=[[[YurSecondViewController alloc]init];
View.img=[UIImage imageNamed:@"Default.png"];
[self.navigationController pushViewController:View animated:YES];

In your YurSecondViewController.m where you have to show image do:-

UIImageView *imgView=[[UIImageView alloc]initWithFrame:CGRectMake(50, 50, 100, 100)];
[imgView setImage:self.img];
[self.view addSubview:imgView];

Upvotes: 1

Related Questions