Skip to content

Instantly share code, notes, and snippets.

@Bogidon
Last active August 18, 2016 01:05
Show Gist options
  • Save Bogidon/28435cc907197ae47aeee84b635c414b to your computer and use it in GitHub Desktop.
Save Bogidon/28435cc907197ae47aeee84b635c414b to your computer and use it in GitHub Desktop.
EasyStoryboards – a better way to manage storyboards and avoid merge conflicts
//
// Storyboards.swift
//
// Created by Bogdan Vitoc on 7/5/16.
// Copyright © 2016 Bogdan Vitoc. All rights reserved.
//
// ⚠️ this is an example only
// ⚠️ modify the properties and strings to suit your project
import UIKit
struct ESBStoryboards {
static let onboarding = UIStoryboard(name: "Onboarding", bundle: nil)
static let login = UIStoryboard(name: "Login", bundle: nil)
static let home = UIStoryboard(name: "Home", bundle: nil)
static let settings = UIStoryboard(name: "Settings", bundle: nil)
static let example = UIStoryboard(name: "Example", bundle: nil)
// ...
static func storyBoardForClass<T>(type: T) -> UIStoryboard? {
switch type {
case is OnboardingViewController.Type: return onboarding
case is LoginViewControler.Type: return login
case is HomeViewController.Type: return home
case is SettingsViewController.Type: return settings
case is ExampleViewController.Type: return example
// ...
default: return nil
}
}
}
//
// ExampleViewController.swift
//
// Created by Bogdan Vitoc on 3/5/16.
// Copyright © 2016 Bogdan Vitoc. All rights reserved.
//
// ⚠️ this is an example only
import UIKit
class ExampleViewController: UIViewController, ESBViewControllerConventions {
@IBOutlet weak var aLabel: UILabel!
@IBOutlet weak var aButton: UIButton!
@IBOutlet weak var anImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// do things like
aLabel.text = "Hi"
}
}
//
// UIViewController+ESBConventions.swift
//
// Created by Bogdan Vitoc on 7/5/16.
// Copyright © 2016 Bogdan Vitoc. All rights reserved.
//
import UIKit
protocol ESBViewControllerConventions {}
extension ESBViewControllerConventions {
static func initFromStoryboard() -> Self? {
return ESBStoryboards.storyBoardForClass(Self)?.instantiateInitialViewController() as? Self
}
}
@Bogidon
Copy link
Author

Bogidon commented Aug 18, 2016

Separate each view controller into it's own .storyboard file and use the name of that file in ESBStoryboards.swift. Also make that view controller the initial view controller of the storyboard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment