To avoid using singleton models in your iPhone/iPad apps, it’s necessary to create the root view programmatically. This way you’re able to inject the model at creation time instead of in the viewDidLoad method.
Here’s how to set up a navigation-based application in code in XCode 4.2 (with automatic memory management enabled).
In the application:didFinishLaunchingWithOptions method of the application delegate (AppDelegate.m file), insert the following code:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
self.myViewController = [[MyView alloc] initWithNibName:@”MyView” bundle:nil];
self.myViewController.myAppModel = <create and set the application model here>;
UINavigationController *navigationController = [[UINavigationController alloc]
Note that it’s important to keep the root view controller (myViewController in the above example) as a retained property in the application delegate. Otherwise the automatic memory handling may release it prematurely in which case your app may fail with an EXC_BAD_ACCESS message code. Se the following link for more information:
@property (strong, nonatomic) MyView *myViewController;