0

我有一个视图控制器 (VC0),其中包含 3 个容器视图,分别是“左”视图控制器 (VC1)、“右”视图控制器 (VC2) 和“中心”集合视图控制器 (VC3)。

VC0 嵌入到导航控制器中,并配置了“左”和“右”按钮。按下每个按钮时,中心视图 (VC3) 将向左或向右滑动,显示下方 VC1 或 VC2 上的菜单。

在右侧的“菜单”视图控制器(VC2)上,我想要一个“编辑”按钮。当按下“编辑”按钮时,我希望中心面板“折叠”并移回其默认位置(不会向左或向右滑动),并且要在集合视图中的所有单元格上切换视图的可见性控制器(VC3)。目标是在每个单元格上放置一个“删除”按钮,当按下“编辑”按钮时该按钮将变为可见,因此可以通过按下每个单元格上的“删除”按钮一次删除一个单元格,并在用户已完成删除单元格。

我可以使用委托获得编辑按钮以折叠中心视图,但我不确定在按下编辑按钮时如何更改所有单元格上的视图的可见性。我尝试过使用委托、闭包以及两者的组合,但没有任何运气。这是我到目前为止所拥有的:

//VC0 - HOLDS CONTAINER VIEWS
import UIKit
class ContainerViewController: UIViewController, RightPanelDelegate {

    func hideLabel() {
        let vc = CenterCollectionCV()
        vc.hideLabel()
    }

    func collapsePanel() {
        animateCenterPanelXPosition(targetPosition: 0, duration: 0.5, springDamping: 0.6) { _ in
            self.currentState = .bothCollapsed
            self.leftView.isHidden = true
            self.rightView.isHidden = true
        }
    }
}
// VC2 - RIGHT CONTAINER
import UIKit

protocol RightPanelDelegate {
    func collapsePanel()
    func hideLabel()
}

class RightPanelVC: UIViewController {

    var delegate: RightPanelDelegate?

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func buttonPressed(_ sender: Any) {
        delegate?.collapsePanel()
        delegate?.hideLabel()   
    }
}
//VC3 - CENTER CONTAINER
import UIKit

private let reuseIdentifier = "Cell"

class CenterCollectionCV: UICollectionViewController {

        func hideLabel() {
        // I'VE TRIED TO ACCESS THE CELL TO HIDE THE LABEL
        // I'VE TRIED USING A CLOSURE IN THE CELL
        // I'VE TRIED USING A DELEGATE IN THE CELL
        // I CAN'T FIGURE OUT THE STEPS OR WORKFLOW REQUIRED
        // COMMON ERROR IS "Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value" which I think will be because I'm not targeting an already instantiated cell but instead creating a new cell instance, eg: 

        // myCell = CenterCell()
        // myCell.label.isHidden = true

// But all the other approaches I've tried haven't worked either.

    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 1
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! CenterCell

        return cell
    }
}
// COLLECTION VIEW CELL
import UIKit

class CenterCell: UICollectionViewCell {

    @IBOutlet var label: UILabel!
}

我希望最终结果是在另一个不相关的容器中按下按钮时隐藏或显示容器中单元格的视图。

任何提示或帮助将不胜感激,谢谢。

4

1 回答 1

0

答案是在 VC0(具有多个容器的视图)中创建一个委托并在 VC3(collectionView)上实现它,我已经尝试过,但关键是在 VC0 和 VC3 之间添加一个 segue,将委托设置为VC0 到 VC3。一旦我这样做了,事情就奏效了。所以最终的答案是我没有正确设置委托。

代码现在如下:

//VC0 - HOLDS CONTAINER VIEWS
import UIKit

protocol ContainerViewControllerDelegate {
    func actionToPerform()
}

class ContainerViewController: UIViewController, RightPanelDelegate {

    var delegate: ContainerViewControllerDelegate?

    func hideLabel() {
        let vc = CenterCollectionCV()
        vc.hideLabel()
    }

    func collapsePanel() {
        animateCenterPanelXPosition(targetPosition: 0, duration: 0.5, springDamping: 0.6) { _ in
            self.currentState = .bothCollapsed
            self.leftView.isHidden = true
            self.rightView.isHidden = true
        }
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        if segue.destination is RightPanelVC {
            let vc: RightPanelVC = segue.destination as! RightPanelVC
            vc.delegate = self
        }

        if segue.destination is ViewController {
            let vc: ViewController = segue.destination as! ViewController
            self.delegate = vc
        }
    }
}
//VC3 - CENTER CONTAINER
import UIKit

private let reuseIdentifier = "Cell"

class CenterCollectionCV: UICollectionViewController, ContainerViewControllerDelegate {

    func actionToPerform() {
    // perform tasks related to collectionView or cell here
    collectionView.isHidden = true
    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 1
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! CenterCell

        return cell
    }
}
于 2019-09-25T22:58:50.970 回答