有一个基类符合 ViewModelInsertProtocol
和函数 insertToCollection()
。 delegate
在 CollectionViewController
类的 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,我必须转到另一个 popVC PopVC 和我的基类有 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/