Here’s what I’ve got:
I have a root view controller with an array of images. I have a detailViewController which simply has a fullscreen UIImageView for displaying a full screen image. I also have a segue defined that presents the detailViewController. This segue is programmatically invoked when the user selects an image in the rootViewController like so:
- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index
{
[self performSegueWithIdentifier:@"fullScreenSegue" sender:self];
}
The problem is that the detailViewController has an image property that it sets as the image for the UIImageView it displays and I need a way to pass the image to it to display. How do I do this? According to apple docs, I should implement the prepareForSegue method in the calling controller, but at that point, I no longer have access to the index above. The images are in an array in the root view controller and I need that index to know which image to pass to the detailViewController. I also set the detailViewController to have a protocol that sets the calling controller as the delegate and then calls a “setImage” method in the init() method, but that doesn’t even seem to work and seems like a crazy implementation anyway…
I thought about declaring a property in the rootViewController like @property int selectedIndex, then setting that property in the above method and then in the prepareForSegue method doing something like:
detailViewController.imageForDisplay = [images objectAtIndex:selectedIndex];
That would probably work, but that approach rubs me wrong…
Thanks in advance for the help!
Two ways to handle it. One use prepareForSegue and save your index somewhere before you call the segue…
then in the
prepareForSegueuse:Second way is to use your delegate and protocol, but make the
setImagecall in yourviewDidLoadmethod, not in yourinit.Hope that helps. It seems like you were getting pretty close.