ylliX - Online Advertising Network

Sharing data between UIViewControllers

If you are new to iOS programming this may take you a while to found out. But I’m here to help so will give you ready to use solution. So you have two (or more) view controllers (UIViewController) and one of them should show list of items, second one details of selected item? Well, solution depends on the way you are doing transitions betweeen controllers.

If you are using pushViewController method then in details view controller header file (for example ShowDetailsController.h) you need to create new property:

@property(nonatomic) BOOL *showImage;

Next step is to import this header file in you main controller (for example ShowItemsController.h), so at top put:

#import "ShowDetailsController.h"

then in your transition action (after button push or table cell touch) put:

ShowDetailsController *detailsController = [[ShowDetailsController alloc] initWithNib:@"ShowDetailsController" bundle:nil];
detailsController.showImage = YES;
[self pushViewController:detailsController animated:YES];

and thats all, in ShowItemsController you will have access to showImage variable.

If you are using segues (and you should) the for sure you have:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

somewhere in your ShowItemsController. Just add new property and import header file like in previous example, then in this prepareForSegue method add:

if([segue.identifier isEqualToString:@"showDetailSegue"]){
    ShowDetailsController *controller = (ShowDetailsController*)segue.destinationViewController;
    controller.showImage = YES;

of course if your segue has different identifier you need to change “showDetailSegue” string according to your own name.

Leave a Reply