Created
March 15, 2016 07:02
-
-
Save miguelfermin/c9cbe813a00af1a171f6 to your computer and use it in GitHub Desktop.
MAFStoryBoardHandlerType.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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