views:

206

answers:

1

My app has a main screen that the user always starts at, and from which I want to display other views programmatically. I set up the app identically to the approach in "Beginning iPhone Development" Ch. 6, which is to use a RootViewController that loads in other view controllers.

The book uses a button to trigger loading the next view controller, but in my app I need to swap controllers at the end of function calls and to share data (processed UIImages, etc) between views. I am not using a tab bar or navigation controller.

What I'm wondering is, should I just make my MainViewController the root controller and presentModalViewControllers from there? I'd like to keep the root model but I don't quite understand how to hook it all up and share data. I've seen posts that mention using protocols and notifications but I haven't wrapped my head around it yet. Any advice is appreciated.

+1  A: 

What you want to do is add a Cocoa property in your main view controller that references the object instances which you want to share with subordinate view controllers.

For example, if we want to share an NSArray, we specify its property in the main view controller header:

@interface MainViewController : UIViewController {
  NSArray *myArray;
}

@property (nonatomic, retain) NSArray *myArray;

@end

In the implementation, add the @synthesize directive and remember to release the array in -dealloc:

@implementation MainViewController

@synthesize myArray;

...

- (void) dealloc {
  [myArray release];
  [super dealloc];
}

@end

You also want to add this property to view controllers that are subordinate to the main view controller, in the exact same way. In their headers, specify the same variable name and property description.

In your main view controller, when you are ready to push a subordinate view controller, you set the subordinate view controller's property accordingly just before pushing:

- (void) pushSubordinateViewController {
  SubordinateViewController *subVC = [[SubordinateViewController alloc] initWithNibName:@"SubordinateViewController" bundle:nil];
  subVC.myArray = self.myArray; // this sets the sub view controller's myArray property
  [self.navigationController pushViewController:subVC animated:YES];
  [subVC release];
}

Likewise in your subordinate view controller, it will need to set its subordinate's array property accordingly, just before it pushes its own sub-sub-view controller.

By setting references in this way, each view controller is pointed at the same array, containing the desired elements.

To use the array, just call self.myArray, e.g. [self.myArray objectAtIndex:index] to get an object at a given index.

Alex Reynolds
That does clear up how to share data between view controllers, thank you. What I'm still unclear on (even after reading Apple's docs) is the use of protocol. Specifically, if you use the Xcode "Utility App" template to create an app it is these lines that confuse me:@protocol FlipsideViewControllerDelegate;@interface FlipsideViewController : UIViewController { id <FlipsideViewControllerDelegate> delegate;}I don't understand why this delegate variable is set up. It looks like it's used in the presenting/dismissing of the flipside view.
Ian