Created
March 14, 2017 20:09
-
-
Save CanTheAlmighty/4baf9384028fdc6709846a217f7f1fc9 to your computer and use it in GitHub Desktop.
Loading files from xib files with the same name
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
// 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