从视图模型结构中获取值

时间:2017-11-13 22:46:41

标签: ios swift

我对swift很新。目前,我试图在Swift 4中以编程方式创建一个视图屏幕。该屏幕应该有许多图像和文本标签。我创建了一个视图模型,其中包含几个声明的图像和字符串的结构。

我的视图模型中的结构设置:

import Foundation
import UIKit

struct ShareUIModel {

    let heroImage: UIImage
    let secondHeroImage: UIImage
    let longHeroText: String
    let shortHeroText: String

}

extension ShareUIModel {

    static func testObject() -> ShareUIModel {
        let heroImageImage = placeholderImage
        let secondHeroImageImage = placeholderImage
        let longHeroTextText = "long words"
        let shortHeroTextText = "short words"
        return ShareUIModel(heroImage: heroImageImage, secondHeroImageImage: secondHeroImage, longHeroText: longHeroTextText, shortHeroText: shortHeroTextText)
    }
}

问题:如何在视图控制器中创建ShareUIModel实例并访问这些值?

1 个答案:

答案 0 :(得分:5)

通常,您可以通过多种方式将MVM对象应用于其视图。 Personnaly,我建议使用convenience初始值设定项,或者使用名为didSet值观察者的自定义变量。在应用参数时调用Didset观察者,而不修改存储值。

所以给定一个可以处理模型的uivew子类:

class ShareUIView : UIView {


    var model : ShareUIModel? {
        didSet {
            guard let mode = model else { return }
            applyModel(mode)
        }
    }

    let heroImage = UIImageView()
    let secondHeroImage = UIImageView()
    let longHeroLabel = UILabel()
    let shortHeroLabel = UILabel()

    //Initializer
    convenience init(model: ShareUIModel) {
        self.init()
        self.addSubview(heroImage)
        self.addSubview(secondHeroImage)
        self.addSubview(longHeroLabel)
        self.addSubview(shortHeroLabel)
        self.model = model
    }

    private func applyModel(_ model: ShareUIModel) {
        self.heroImage.image = model.heroImage
        self.secondHeroImage.image = model.heroImage
        self.longHeroLabel.text = model.longHeroText
        self.shortHeroLabel.text = model.shortHeroText
    }

    override func draw(_ rect: CGRect) {
        super.draw(rect)
        buildYourComponents()
    }

    func buildYourComponents() {
        //This will be called whenever ShareUIView's size or layout is applied.
        //code to build out your labels and imageViews
    }

}

从这里开始,您可以通过初始化将其应用到UIViewController view上:

class Controller : UIViewController {


    override func viewDidLoad() {
        super.viewDidLoad()

        let shareview = ShareUIView(model: ShareUIModel.testObject())
        view.addSubview(shareview)
        shareview.frame = //whatever you want here..
    }

}

此方法意味着您必须在之后设置大小。

或者,您可以先构建ShareView,然后将模型应用到ShareView&#39,然后应用模型对象(例如,从用户的选择或网络任务中) ; s模型参数。这要求您对它有一个强引用,就像在初始化它到Controller类中一样:

class Controller : UIViewController {

    var shareView : ShareUIView = { //Initialized as a parameter of Controller instead
        var v = ShareUIView()
        return v
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(shareView) //You add the view
        shareView.frame = .zero//you build the view - .zero is just an example here.
    }

    func applyTheModelFromWherever() {
        shareView.model = ShareUIModel.testObject() //you apply the model object.
    }

}

通过这个,您应该有足够的示例来了解如何使用它的模型对象内容更新MVVM视图。然后,您还可以创建方法来清除内容,忽略它等,具体取决于视图本身的用例。