【问题标题】:Loading iOS view dynamically from xib file从 xib 文件动态加载 iOS 视图
【发布时间】:2017-05-11 21:16:58
【问题描述】:

我一直在寻找一个简单的例子,说明如何在我的故事板中重用 xib 文件中的视图,但我发现的都是过时的或无法解决我的问题 我的情况很简单:

  • 我的故事板中有一个 viewController
  • 我从库中拖了两个视图
  • 我创建了一个 myCustomView.xib 和 myCustomView.swift 文件(它们现在是空的)
  • 我在 viewController 上有 I 按钮(因此树(两个视图和一个按钮)在情节提要的 viewController 上一起设置)
  • 问题是:我希望在应用启动时动态加载一个视图,而在单击按钮时加载另一个视图
  • 另一个问题:如何将 myCustomView 连接到该 viewController

谢谢

【问题讨论】:

    标签: ios swift xcode


    【解决方案1】:

    我已经为 UIView 实现了一个扩展:

    extension UIView {
    
        static func createInstance<T: UIView>(ofType type: T.Type) -> T {
            let className = NSStringFromClass(type).components(separatedBy: ".").last
            return Bundle.main.loadNibNamed(className!, owner: self, options: nil)![0] as! T
        }
    
    }
    

    通过这种方式,无论您在哪里都可以通过这种方式加载自定义视图:

    func override viewDidLoad() {
        super.viewDidLoad()
        let customView = UIView.createInstance(ofType: CustomView.self) 
        self.view.addSubview(customView)
    }
    

    【讨论】:

    • 如果我的自定义视图上有一个按钮!我会把按钮的处理程序放在哪里?在 myCustomView 或实例化视图的 viewController 上
    • 当然是在您的 customView 中。然后你有几种方法来“通知”相关的视图控制器。
    • 不错的答案,但type 参数真的有必要吗?您不能从通用参数T 中推断出这一点吗?也许使用T.self
    • 我无法让它运行这条线let customView = UIView.createInstance(ofType: CustomView.self)Cannot convert type UIView! to expected argument type UIView.Type
    【解决方案2】:

    在您的自定义视图类中添加波纹管代码

    class MyCustomView: UIView {
    
    
       @IBOutlet var contentView: UIView! // take view outlet
    
        override init(frame: CGRect) {
            super.init(frame: frame)
            xibSetup()
        }
    
        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
            xibSetup()
        }
    
    
        func xibSetup() {
            contentView = loadViewFromNib()
    
            // use bounds not frame or it'll be offset
            contentView!.frame = bounds
    
            //Make the view stretch with containing view
            contentView!.autoresizingMask = [UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight]
    
            // Adding custom subview on top of our view (over any custom drawing > see note below)
            addSubview(contentView!)
            layoutIfNeeded()
        }
    
        override func layoutIfNeeded() {
            super.layoutIfNeeded()
            print("layoutIfNeeded")
    
    
        }
    
        func loadViewFromNib() -> UIView! {
            let bundle = Bundle(for: type(of: self))
            let nib = UINib(nibName: String(describing: type(of: self)), bundle: bundle)
            let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
            return view
        }
    }
    

    将此类添加为故事板中的超类视图。

    【讨论】:

    • 我看到这是myCustomView.swift,如果我想在viewController 中的某个地方实例化这个视图我该怎么办?
    • 这很好用!当然,您需要先正确链接您的笔尖,但我在设置这部分时遇到了麻烦。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-21
    • 2019-07-04
    • 2016-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多