Setting Up A Navigation-based Application Programmatically

Problem:

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.

Solution:

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:

[objective-c]

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]
initWithRootViewController:self.myViewController];
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;

[/objective-c]

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:

http://stackoverflow.com/questions/2070204/exc-bad-access-with-ibaction

[objective-c]

@property (strong, nonatomic) MyView *myViewController;

[/objective-c]

RSS feed | Trackback URI

Comments »

No comments yet.

Name (required)
E-mail (required - never shown publicly)
URI
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> in your comment.