Skip to content

Instantly share code, notes, and snippets.

@Superbil
Last active March 8, 2019 09:25
Show Gist options
  • Select an option

  • Save Superbil/54d4ccc57f627e93ae786227932d1eb2 to your computer and use it in GitHub Desktop.

Select an option

Save Superbil/54d4ccc57f627e93ae786227932d1eb2 to your computer and use it in GitHub Desktop.
Easy to create ViewController
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 {}
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