swift - 让容器 View 上的按钮对不相关的容器 View 执行操作

标签 swift uicollectionview delegates uicollectionviewcell

我有一个 View Controller (VC0),其中包含 3 个容器 View ,即“左” View Controller (VC1)、“右” View Controller (VC2) 和一个“中心” Collection View Controller (VC3) .

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

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

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

//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!
}

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

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

最佳答案

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

代码现在如下:

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

关于swift - 让容器 View 上的按钮对不相关的容器 View 执行操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58044769/

相关文章:

ios - UICollectionView 和补充 View (标题)

ios - UICollectionView 单元格消失

ios - 类型 MyViewController 不符合协议(protocol) 'STPPaymentContextDelegate'

iphone - 实现 NSURLConnectionDataDelegate 协议(protocol)

swift - 创建无图像的无尽背景

ios - 获取请求 "unrecognized selector"错误

ios - UICollectionView 粘性标题在集合过度滚动时插入部分后消失一段时间(弹跳效果)

c# - 将一种类型的委托(delegate)动态转换为另一种

swift - 带有组合框的 mapkit 自动完成

Swift:将 ALAsset 复制到应用程序的文档目录中