Skip to content

RogyMD/SwiftyStoryboard

Repository files navigation

SwiftyStoryboard

A framework that helps you to operate with UIStoryboard and it’s components.

Swift version Pod Version GitHub license

Requirements

  • iOS 8.0+
  • Xcode 8.1+
  • Swift 3.0+

Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

To integrate SwiftyStoryboard into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!

target '<Your Target Name>' do
    pod 'SwiftyStoryboard'
end

Then, run the following command:

$ pod install

Usage

Create a StoryboardType

import SwiftyStoryboard

// Enum example of `StoryboardType`.
enum AppStoryboard: String, StoryboardType {
  case main = "Main"
}

// Struct example of `StoryboardType`.
struct AppStoryboardStruct: RawRepresentable, StoryboardType {
  typealias RawValue = String
  
  let rawValue: String
  
  init(rawValue: String) {
    self.rawValue = rawValue
  }
  
  static var main = AppStoryboardStruct(rawValue: "Main")
}

Create a StoryboardSceneType

import SwiftyStoryboard

class ViewController: UIViewController {}

extension ViewController: StoryboardSceneType {
  class var mainStoryboard: AppStoryboard {
    return .main
  }
}

StoryboardSceneType provides default class property scene. Feel free to use it to initialize an ViewController instance.

let vc = ViewController.scene

Old:

let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController") as! ViewController

Create a SeguePerformer

import SwiftyStoryboard

class ViewController: UIViewController {}

extension ViewController: SeguePerformer {
  // Define `SegueType` as nested enum `Segue`
  typealias SegueType = Segue
  
  // Nested enum `Segue` with defined `UIStoryboardSegue` identifier as implicit `rawValue`
  enum Segue: String, StoryboardSegueType {
    case second
  }
}

Perform segues easier.

perform(segue: .second)

Old:

performSegue(withIdentifier: "second", sender: nil)

Handle segues in more swifty way.

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    super.prepare(for: segue, sender: sender)
    
    // `Segue` should be initialized with `UIStoryboardSegue` instance.
    if let segue = Segue(segue) {
      NSLog("Segue('\(segue.segueID)') was performed.")
    }

    // Old
    if let identifier = segue.identifier, identifier == "second" {
      NSLog("Segue('\(identifier)') was performed.")
    }
}

License

SwiftyStoryboard is released under the MIT license. See LICENSE for details.