Ios 委托为零,MVVM方法,两个VC具有相同的VM。斯威夫特
有一个基类使用函数Ios 委托为零,MVVM方法,两个VC具有相同的VM。斯威夫特,ios,swift,mvvm,Ios,Swift,Mvvm,有一个基类使用函数insertToCollection()符合ViewModelInsertProtocol。在CollectionViewController类的viewDidLoad方法中将委托设置为self class CollectionViewController: UIViewController, ViewModelInsertProtocol { func insertToCollection() { print("asdasd") collection.pe
insertToCollection()
符合ViewModelInsertProtocol
。在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 viewModel=viewModel()
我对这两个类使用相同的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()
}
var viewModel=viewModel()
//马克:生命周期
重写func viewDidLoad(){
super.viewDidLoad()
配置()
addObservers()
viewModel.popDelegate=self
}
覆盖函数视图将消失(u动画:Bool){
viewModel.editSelected=false
}
func configure(){
saveBtnOutlet.visualButton()
xBtnOutlet.visualButton()
cancelBtnOutlet.visualButton()
firstCardColor.visualButton()
secondCardColor.visualButton()
}
@iAction func CancelBtn(\发送方:任意){
驳回(动画:真,完成:无)
}
@iAction func saveBtn(\发送方:任意){
viewModel.saveData(文本:textF.text)
驳回(动画:真,完成:无)
}
rotocol ViewModelInsertProtocol:类{
func insertToCollection()
}
扩展视图模型{
func numberOfRows()->Int{
返回notes.count
}
}
类视图模型{
var注释:结果{
得到{
返回realm.objects(Model.self)
}
}
弱var委托:ViewModelInsertProtocol?=nil
弱变量popDelegate:popDelegate?=nil
var backgroundImageIndex=0
var editSelected=false
让realm=try!realm()
var size=size.largeSize
变量背景:String=“wood”
func saveData(文本:字符串){
设newcard=Model()
newcard.note=文本
realmservice.shared.create(新卡)
打印(备注.计数)
如果委托==nil{print(“nil”)}
委托?.insertToCollection()
}
此代码有什么问题?遵循MVVM方法和依赖项注入,您可以注入ViewModel,而不是在pop控制器中创建新实例,因此,例如,如果您使用的是情节提要,请在“准备阶段”:
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)
}
我在我的应用程序中发现了接下来的两个问题。
1.我试着和第一个问题一样,不插入新单元格,而是删除单元格,为了实现这一点,我在PopVC中创建了协议,在基类中进行了验证,并在prepareForSegueMethod中设置了委托。这对于第一个任务来说非常有效。我的意思是cellInsert,但对于delete不起作用。区别在于当我想删除我的c通过didSelectItemAtIndexPath帮助将信息发送到PopVC,在插入的情况下,我使用加号按钮。是否需要在didSelectItemAtIndexPah中设置单元格的委派
2单元格选择问题:我无法在collectionView中选择第一个单元格,在didselectItemForIndePath中,我使用方法print(indexPath.row)。问题是此打印显示错误的行。例如,如果我选择第二行,print显示1或3行。一次打印“1”,另一次打印“0”,另一次打印“5”你知道这里发生了什么吗?在PopVC类下面,我用协议粘贴了viewModel类。你能说得更清楚吗?我没有明白你的要求。好的。我有两个ViewController,一个VC是基本的,第二个类似于popUp。两者都连接到一个viewModel,并在se类。如果我想添加新单元格,我需要单击PopVC中的savBtn,该方法从ViewModel中创建一个对象,然后通过委托方法在基类中插入新单元格。更清楚的是?在PopVC中编写协议并使基类符合该协议。然后,u应该能够回调并获取您想要的任何数据在函数u中可以添加新的uicollectionView单元格方法。这与mvvm方法正确吗?之前我认为我的POPVC必须先连接ViewModel,ViewModel将更新然后是基类。因此,在你的方法中,我们将在两个VC中使用相同的ViewModel实例正确吗?对于我来说,有什么区别吗e同一个实例并在每个类中创建了两个单独的实例?我给你加号是因为我之前不知道这种想法。THX基本控制器viewModel的委托为零,因为你是从viewModel类的新实例调用saveData函数的。