Skip to content

Instantly share code, notes, and snippets.

@MaciejGad
Created July 26, 2017 12:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MaciejGad/74aaa503d4c9e67b76764dca6cca13b8 to your computer and use it in GitHub Desktop.
Save MaciejGad/74aaa503d4c9e67b76764dca6cca13b8 to your computer and use it in GitHub Desktop.
NibBaseViewController
import Foundation
import UIKit
enum NibBaseError:Error {
case badClassName
case noNibFile
case noStoryboard
}
class NibBaseViewController: UIViewController, IsNibBaseViewController {
required init(_ : Void) throws {
let classNameComponents = NSStringFromClass(type(of: self)).components(separatedBy: ".")
guard let className = classNameComponents.last else {
throw NibBaseError.badClassName
}
let bundle = Bundle(for:type(of:self))
guard bundle.path(forResource: className, ofType: "nib") != nil else {
throw NibBaseError.noNibFile
}
super.init(nibName:className, bundle: Bundle(for:type(of:self)))
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
var isNavigationBarHidden:Bool? {
return nil
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let isNavigationBarHidden = isNavigationBarHidden {
navigationController?.setNavigationBarHidden(isNavigationBarHidden, animated: true)
}
}
}
protocol HavingRequiredThrowingInit {
init(_ : Void) throws
}
protocol IsNibBaseViewController: HavingRequiredThrowingInit, IsViewController {}
protocol IsViewController {
func asViewController() -> UIViewController
}
extension UIViewController: IsViewController {
func asViewController() -> UIViewController {
return self
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment