Skip to content

Instantly share code, notes, and snippets.

@miguelfermin
Created March 15, 2016 07:02
Show Gist options
  • Save miguelfermin/c9cbe813a00af1a171f6 to your computer and use it in GitHub Desktop.
Save miguelfermin/c9cbe813a00af1a171f6 to your computer and use it in GitHub Desktop.
MAFStoryBoardHandlerType.
//
// MAFStoryBoardHandlerType.swift
// MAFFinance
//
// Created by Miguel Fermin on 10/20/15.
// Copyright © 2015 MAF Software LLC. All rights reserved.
//
import UIKit
protocol MAFStoryBoardHandlerType {
typealias StoryBoardIdentifier: RawRepresentable
}
extension MAFStoryBoardHandlerType where Self: UIViewController, StoryBoardIdentifier.RawValue == String {
/// Instantiates and returns the view controller with the specified identifier.
/// - parameter identifier: An identifier StoryBoardIdentifier enum type that uniquely identifies the view controller in the storyboard file.
/// - returns: The view controller corresponding to the specified identifier, StoryBoardIdentifier enum type.
func instantiateViewControllerWithIdentifier(identifier: StoryBoardIdentifier) -> UIViewController {
guard let storyboard = self.storyboard
else { fatalError("Unable to instantiate view controller with identifier \(identifier.rawValue) from Storyboard.") }
return storyboard.instantiateViewControllerWithIdentifier(identifier.rawValue)
}
/// Instantiates and returns the navigation controller with the specified identifier.
/// - parameter identifier: An identifier StoryBoardIdentifier enum type that uniquely identifies the navigation controller in the storyboard file.
/// - returns: The navigation controller corresponding to the specified identifier, StoryBoardIdentifier enum type.
/// - note: If the identifier doesn't correspond to a UINavigationController, this method returns nil.
func instantiateNavigationControllerWithIdentifier(identifier: StoryBoardIdentifier) -> UINavigationController? {
return self.instantiateViewControllerWithIdentifier(identifier) as? UINavigationController
}
}
/* How to use it */
// Your view controller must conform to the protocol
class MAFViewController: MAFDetailViewController, MAFStoryBoardHandlerType
// Then you must implement the "StoryBoardIdentifier" enum
enum StoryBoardIdentifier: String {
case InAppPurchasePage = "MAFIAPurchasePageStoryboardID"
}
// Then you can use it as follows
if let viewController = instantiateViewControllerWithIdentifier(.InAppPurchasePage) as? InAppPurchaseViewController {
// Use viewController
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment