gpt4 book ai didi

ios - 委派为 nil ,MVVM 方法,两个 VC 具有相同的 VM。 iOS, swift

转载 作者:行者123 更新时间:2023-11-28 05:36:16 25 4
gpt4 key购买 nike

有一个基类符合 ViewModelInsertProtocol 和函数 insertToCollection()delegateCollectionViewController 类的 viewDidLoad 方法中设置为 self。

class CollectionViewController: UIViewController, ViewModelInsertProtocol {


func insertToCollection() {
print("asdasd")
collection.performBatchUpdates({
collection.insertItems(at: [IndexPath(row: viewModel.notes.count - 1, section: 0)])
}, completion: nil)
}


//MARK: -Variables

let reuseIdentifier = "cell"
var isEdit = false
var isSettingsEdit = false
var viewModel = ViewModel()


//MARK: -OUTLETS

@IBOutlet var mainView: UIView!
@IBOutlet weak var bacgroundImageView: UIImageView!
@IBOutlet weak var collection: UICollectionView!
@IBOutlet weak var addButton: UIButton!
@IBOutlet weak var sideViewConstraint: NSLayoutConstraint!
@IBOutlet weak var settingConstraint: NSLayoutConstraint!
@IBOutlet weak var sideView: UIView!
@IBOutlet weak var icon: UIButton!
@IBOutlet weak var settingButton: UIButton!
@IBOutlet weak var backgr: UIButton!
//MARK: - LifeCycle

override func viewDidLoad() {
super.viewDidLoad()


configureUI()
viewModel.delegate = self
}

如果我想添加新的 UICollectionCell,我必须转到另一个 popVCPopVC 和我的基类有 viewModel 的实例

var View 模型 = View 模型()我对这两个类使用相同的 viewModel,看看 PopVC 中的 SaveBtn,在这个地方我使用委托(delegate)方法。

var viewModel = ViewModel()


//MARK: -Lifecycle

override func viewDidLoad() {
super.viewDidLoad()

configure()
addObservers()
viewModel.popDelegate = self


}
override func viewWillDisappear(_ animated: Bool) {
viewModel.editSelected = false
}

func configure() {
saveBtnOutlet.visualButton()
xBtnOutlet.visualButton()
cancelBtnOutlet.visualButton()
firstCardColor.visualButton()
secondCardColor.visualButton()

}


@IBAction func CancelBtn(_ sender: Any) {
dismiss(animated: true, completion: nil)
}

@IBAction func saveBtn(_ sender: Any) {

viewModel.saveData(text: textF.text)
dismiss(animated: true, completion: nil)

}


rotocol ViewModelInsertProtocol: class {
func insertToCollection()
}

extension ViewModel {

func numberOfRows() -> Int {
return notes.count
}

}

class ViewModel {

var notes: Results<Model> {
get{
return realm.objects(Model.self)
}
}
weak var delegate : ViewModelInsertProtocol? = nil
weak var popDelegate: PopDelegate? = nil
var backgroundImageIndex = 0
var editSelected = false
let realm = try! Realm()

var size = Sizes.largeSize
var background: String = "wood"

func saveData(text: String) {
let newcard = Model()
newcard.note = text
RealmSerivce.shared.create(newcard)
print(notes.count)
if delegate == nil {print("nil")}
delegate?.insertToCollection()

}

这段代码有什么问题?

最佳答案

按照 MVVM 方法和依赖注入(inject),您可以注入(inject) ViewModel 而不是在弹出 Controller 中创建新实例,例如,如果您使用 Storyboard,则在准备 segue 时:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "PopVcIdentifier" {
let popController = segue.destination as? PopVC
popController?.viewModel = viewModel
}
}

或者在 showPopUp 函数中:

func showPopUp() {
let popController = PopVC()
popController.viewModel = viewModel
present(popController, animated: true)
}

关于ios - 委派为 nil ,MVVM 方法,两个 VC 具有相同的 VM。 iOS, swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58554867/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com