Last active
March 8, 2019 09:25
-
-
Save Superbil/54d4ccc57f627e93ae786227932d1eb2 to your computer and use it in GitHub Desktop.
Easy to create ViewController
This file contains hidden or 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
| import Foundation | |
| protocol ClassNameProtocol { | |
| static var className: String { get } | |
| var className: String { get } | |
| } | |
| extension ClassNameProtocol { | |
| static var className: String { | |
| return String(describing: self) | |
| } | |
| var className: String { | |
| return type(of: self).className | |
| } | |
| } | |
| extension NSObject: ClassNameProtocol {} |
This file contains hidden or 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
| import UIKit | |
| protocol UIViewControllerCreator { | |
| static func viewController() -> Self? | |
| static var bundleName: String? { get } | |
| static var storyboardName: String? { get } | |
| static var nibName: String? { get } | |
| } | |
| extension UIViewControllerCreator where Self: UIViewController { | |
| static func viewController() -> Self? { | |
| var bundle: Bundle? = nil | |
| if bundleName != nil { | |
| bundle = Bundle.init(identifier: bundleName!) | |
| } | |
| if let sbName = storyboardName { | |
| let sb = UIStoryboard.init(name: sbName, bundle: bundle) | |
| // Just lookup identifier from className | |
| return sb.instantiateViewController(withIdentifier: Self.className) as? Self | |
| } | |
| else if let nibName = nibName { | |
| return UIViewController.init(nibName: nibName, bundle: bundle) as? Self | |
| } | |
| return nil | |
| } | |
| static var bundleName: String? { | |
| return nil | |
| } | |
| static var storyboardName: String? { | |
| return nil | |
| } | |
| static var nibName: String? { | |
| return nil | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment