Skip to content

Instantly share code, notes, and snippets.

@CanTheAlmighty
Created March 14, 2017 20:09
Show Gist options
  • Save CanTheAlmighty/4baf9384028fdc6709846a217f7f1fc9 to your computer and use it in GitHub Desktop.
Save CanTheAlmighty/4baf9384028fdc6709846a217f7f1fc9 to your computer and use it in GitHub Desktop.
Loading files from xib files with the same name
// MARK: - Protocol Declaration
public protocol InterfaceBuilderInstantiable
{
/// The UINib that contains the view
///
/// Defaults to the swift class name if not implemented
static var associatedNib : UINib { get }
}
// MARK: - Default Implementation
extension InterfaceBuilderInstantiable
{
/// Creates a new instance from the associated Xib
///
/// - Returns: A new instance of this object loaded from xib
static func instantiateFromInterfaceBuilder() -> Self
{
return associatedNib.instantiate(withOwner:nil, options: nil)[0] as! Self
}
static var associatedNib : UINib
{
let name = String(describing: self)
return UINib(nibName: name, bundle: Bundle.main)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment