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:

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

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

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:

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

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

Leave a Reply