Skip to content

Instantly share code, notes, and snippets.

@alanf
Last active August 29, 2015 14:25
Show Gist options
  • Save alanf/99d210113a3cdb1e4e3c to your computer and use it in GitHub Desktop.
Save alanf/99d210113a3cdb1e4e3c to your computer and use it in GitHub Desktop.
// Magic! Just specify the type and the function on the right hand side will magically get you the correct view from the xib.
let noteView: AlterationReceiptNoteView = ViewUtil.viewFromNib(.JPAlterations)
public enum XIBFileName : String {
case Receipt = "ReceiptViewController"
case USAlterations = "AlterationReceiptUSView"
case JPAlterations = "AlterationReceiptsJapanView"
}
public class func viewFromNib<T>(nibName: XIBFileName) -> T {
let views = UINib(nibName: nibName.rawValue, bundle: nil).instantiateWithOwner(nil, options: nil)
for view in views {
if let desiredView = view as? T {
return desiredView
}
}
fatalError("Should have found a matching view for this type")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment