is a microblog
home archives search feed blogroll

Get Rid Of Storyboards

It doesn’t take very long before storyboards get messy and take a long time to load. It’s also very painful to work on a team project that uses one storyboard. You’d have to ask the other members to not touch any view in the storyboard while you edit it or else the merge could end up being very painful.

I prefer having all my views in separate files. The UNIX philosophy is do one thing and do it well”. My UIView philosophy is do one view and do it well without a million segue lines”.

Who wants to have to deal with segue lines that you can’t rearrange? Apple programmers will tell you that the solution is to use multiple storyboards for your application. I will tell you to go one step further and to not use any storyboard at all.

Tutorial begins here.

Delete the storyboard files

  1. Open your plist file.
  2. Remove the entry Main storyboard file base name.
  3. Delete the storyboard files Xcode created when you started your new project.

Create a new view

This is how you’re going to create new views from now on.

  1. Right click somewhere in your project’s structure and select New file.
  2. In the User Interface section, select View.
  3. Click Next, name your new view and create it.

Create a new view controller.

This is how you’re going to create new view controllers from now on.

  1. Right click somewhere in your project’s structure and select New file.
  2. In the Source section, select Cocoa Touch Class.
  3. Click Next.
  4. Name your new view controller.
  5. In Subclass of:, specify UIViewController.
  1. Open your view file (hint: it’s a .xib file).
  2. On the left, under Placeholders, select File's Owner.
  3. In the Custom Class section, enter your view controller’s name in Class.

Enter View Controller Name

  1. Then add an outlet from your file owner’s view to your view. It’s the hardest step and it’s a bit hard to explain. Do it like in the screenshot below. (hint: you need to keep cmd pressed down).

Add Outlet

Instantiate your initial view controller

In your AppDelegate, import your view controller, then create a new function like the one below.

+ (UIWindow*)createWindow {
  UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  UINavigationController *navigationController = [[UINavigationController alloc] init];
  MainViewController *main = [[MainViewController alloc] init];

  [navigationController addChildViewController:main];
  window.rootViewController = navigationController;

  return window;

MainViewController is how I named my initial view controller. The function doesn’t have to live in AppDelegate, I usually put it in a class called WindowBuilder. Your didFinishLaunchingWithOptions should look something like this.

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
    // Build the app's window and display it.
    self.window = [self createWindow];
    [self.window makeKeyAndVisible];

    return YES;
MyOtherViewController *viewController = [[MyOtherViewController alloc] init];
[self.navigationController pushViewController:viewController animated:YES];


MyOtherViewController *viewController = [[MyOtherViewController alloc] init];
[self.navigationController presentViewController:viewController animated:YES];

That’s all. It’s not very complicated but it took me some time to figure out.

Posted on 2018-03-19   #ios     #objc  

← Next post    ·    Previous post →