This tutorial shows how to hook up an AppDelegate, Dependencies object, a Root wireframe, and a standalone ViewController in a VIPER design pattern.
AppDelegate.h
1 2 3 |
@interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end |
Standard AppDelegate with the window property.
AppDelegate.m
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#import "AppDelegate.h" #import "AppDependencies.h" @interface AppDelegate () { } @property (nonatomic, strong) AppDependencies * dependencies; @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { NSLog(@"%s", __FUNCTION__); AppDependencies *dependencies = [[AppDependencies alloc] initWithWindow:self.window]; self.dependencies = dependencies; [self.dependencies installRootViewController]; return YES; } .. .. |
AppDelegate allocates a Dependencies object. That dependencies object will then install a RootViewController onto the Window object.
Dependencies
The dependencies component has a property wireframe. This wireframe is what carries the logic of showing which ViewController. The ViewControllers are simply instantiated and used by the wireframe.
AppDependencies.h
The installRootViewControllerIntoWindow method simply passes along the window object, and let’s other wireframes (or itself) set view controllers on the window
1 2 3 4 5 6 7 8 9 10 |
#import <Foundation/Foundation.h> @class UIWindow; @interface AppDependencies : NSObject -(id)initWithWindow:(UIWindow*)window; - (void)installRootViewController; @end |
AppDependencies.m
We simply instantiate a ViewController, and then passes it along into the RootWireFrame object so it can do its logic of showing which controller in the window
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
#import <UIKit/UIKit.h> #import "AppDependencies.h" #import "RootWireframe.h" #import "ViewController.h" @interface AppDependencies () @property (nonatomic, strong) RootWireFrame * rootWireFrame; @end @implementation AppDependencies - (id)initWithWindow:(UIWindow *)window { if (self = [super init]) { self.rootWireFrame = [[RootWireFrame alloc] initWithWindow:window]; } return self; } - (void)installRootViewController { NSLog(@"%s", __FUNCTION__); ViewController * rootViewController = [[ViewController alloc] init]; [self.rootWireFrame showRootViewController:rootViewController]; } @end |
Wireframe carries logic of showing ViewControllers/Views
RootWireFrame.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#import <Foundation/Foundation.h> @class UIViewController; @class UIWindow; @interface RootWireFrame : NSObject { } - (instancetype)initWithWindow:(UIWindow*)window; - (void)showRootViewController:(UIViewController *)viewController; @end |
RootWireFrame.m
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
@interface RootWireFrame() { } @property (nonatomic, strong) UINavigationController * nav; @property (nonatomic, strong) UIWindow * window; @end @implementation RootWireFrame -(instancetype)initWithWindow:(UIWindow*)window { if(self = [super init]) { self.window = window; } return self; } //custom getter -(UINavigationController*)nav { if(_nav != nil) { return _nav; } _nav = [[UINavigationController alloc] init]; _nav.navigationBarHidden = YES; self.window.rootViewController = _nav; return _nav; } - (void)showRootViewController:(UIViewController *)viewController { NSLog(@"%s", __FUNCTION__); [self.nav pushViewController:viewController animated:YES]; } @end |