iOS Dev Nugget 252 A Little Trick to Write Loosely Coupled View Controllers


Need to run a code review on your codebase? Hire me

Using in-app routing is a good way to decouple your UI navigation code from your view controllers. It's good to go further and make sure your view controllers don't rely too much on one another. Here's one trick that I have found useful:

When I create a new view controller, I develop and test it by setting my UIWindow instance's rootViewController to an instance of the view controller. e.g.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    //Comment out actual root view controller
    //self.window.rootViewController = [ActualRootViewController new];
    TheNewViewController* vc = [TheNewViewController new];
    //Set up view controller
    vc.photoId = @"123";
    self.window.rootViewController = vc;
    [self.window makeKeyAndVisible];
    return YES;

The first benefit is you can test it immediately when the app launches, making iterations faster. Equally important: it becomes near impossible to write code that is tightly coupled to this new view controller because you are not using it at the actual call site (yet).

Have fun!

Your feedback is valuable: Do you want more nuggets like this?   Yes   or   No



Like this and want such iOS dev nuggets to be emailed to you, weekly?

Sign Me Up! or follow @iosdevnuggets on Twitter


View archives of past issues