swift:如何从xib获取self,loadNib实例化View

Paste_Image.png
class func showHUDAddedToWindowWithText(text: String) -> LEAlertHud {
    let alertHud = self.loadFromNib()
    alertHud.alertContentLabel.text = text
    alertHud.initUI()
    return alertHud
}
Paste_Image.png
// MARK: - How to initialise a UIView Class with a xib file in Swift
extension UIView {
class func loadFromNibNamed(nibNamed: String, bundle : NSBundle? = nil) -> UIView? {
    return UINib(
        nibName: nibNamed,
        bundle: bundle
        ).instantiateWithOwner(nil, options: nil)[0] as? UIView
    }
}

protocol UIViewLoading {}
extension UIView : UIViewLoading {}
extension UIViewLoading where Self : UIView {
// note that this method returns an instance of type `Self`, rather than UIView
static func loadFromNib() -> Self {
    let nibName = "\(self)".characters.split{$0 == "."}.map(String.init).last!
    let nib = UINib(nibName: nibName, bundle: nil)
    return nib.instantiateWithOwner(self, options: nil).first as! Self
}

}
参考文章:
http://stackoverflow.com/questions/24370061/assign-xib-to-the-uiview-in-swift
http://stackoverflow.com/questions/25513271/how-to-initialise-a-uiview-class-with-a-xib-file-in-swift-ios

作者声明:我是小菜鸟,文章内容仅是自己在工作中总结、同事们的指导和网络搜索,难免有疏漏错误,大神们应用之后没有解决问题,我很抱歉;如能即时提出予以雅正,不胜感激.谢绝发泄式差评.欢迎交流分享,祝好.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,496评论 0 23
  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的阅读 14,599评论 5 6
  • 依然在家,没有书看,关注了萧秋水的公众号。先是从达达令的微博上看到的,就先关注了微博,萧每天都发微博,春节期间也没...
    冬素阅读 1,720评论 0 0
  • 冬日的洋槐树象一个长期吃不饱饭的煤矿工人,个子高挑,浑身肌肤干裂,竖立了干枯枯发稍,一身洗不净的灰夹杂着黑,...
    风_行水上阅读 2,701评论 0 2
  • 無法喘息,分分鐘要倒下的眩暈恍惚感一直都有。 好無力。 即使如此,我依然可以保持說笑。 我始終相信,陽光堅強的氣質...
    心的字阅读 1,751评论 1 0

友情链接更多精彩内容