iphoneimage-viewer

Problems setting the correct file name to ImageViewer


I am trying to pass the value of a string from an array to an instance of ImageViewer when building a table view cell, but I am getting nil values. Here is the code:

NSString *file = [[NSString alloc] init ];
file = [photoFiles objectAtIndex:indexPath.row];

ImageViewer *imageView  = [[ImageViewer alloc] initWithNibName:@"ImageViewer" bundle:nil];
imageView.fileName = file;
[self.navigationController pushViewController:imageView animated:YES];
[imageView release];

Can you help me please to fix this problem?


Solution

  • If imageView.fileName = file is setting a nil value, you probably should consider analyzing the contents of the photoFiles array.

    It could be possible that this array has no values at the indexPath.row index. You should check this through the debugger, or with a log print:

    NSLog(@"Array contents: %@", [photoFiles description]);
    

    Edit

    You could write this code in a more concise way. I.e.:

    ImageViewer *imageView  = [[ImageViewer alloc] initWithNibName:@"ImageViewer" bundle:nil];
    imageView.fileName = [photoFiles objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:imageView animated:YES];
    [imageView release];
    

    Also, you should avoid pointing to a nil NSBundle. I.e. Change the first row to:

    ImageViewer *imageView  = [[ImageViewer alloc] initWithNibName:@"ImageViewer" bundle:[NSBundle mainBundle]];
    

    But it should work also this way, since the class name and the Nib name are the same:

    ImageViewer *imageView  = [[ImageViewer alloc] init];
    

    Try it and let me know if something changed.