Getting Values ​​from a Model Structure Model

I am pretty newbie. I'm currently trying to create a view screen programmatically in Swift 4. There should be several images and text labels on the screen. I created a view model with the structure of several declared images and strings.

Setting up the structure that is in my model:

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) } } 

Question How can I instantiate ShareUIModel in the view controller and access the values?

+5
source share
1 answer

Typically, you can apply an MVM object to it in many ways. Personnaly, I recommend either a convenience initializer or a custom variable with what is called an observer of didSet values. Didset watchers invoke when their parameters are applied, without changing the stored value.

Therefore, you are given a subclass of uivew that can process your model:

 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 size or layout is applied. //code to build out your labels and imageViews } } 

So, you can either apply it to your UIViewController view by initializing it:

 class Controller : UIViewController { override func viewDidLoad() { super.viewDidLoad() let shareview = ShareUIView(model: ShareUIModel.testObject()) view.addSubview(shareview) shareview.frame = //whatever you want here.. } } 

This method means you need to set the size afterwards.

Or you can first create a ShareView, and then apply the model object (for example, from a custom selection or from a network task) by applying the model to the ShareView model ShareView . This requires that you have a strong reference to it, as when initializing it in your Controller class:

 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. } } 

From this, you should have enough examples to get creative with how to update the MVVM view with its contents of the model object. Then you also created methods to clear the content, reject it, etc., depending on the use case of the view itself.

+5
source

Source: https://habr.com/ru/post/1273333/


All Articles