繁体   English   中英

我正在尝试将值从UICollectionView项传递到Swift 4中的UIButton

[英]I am trying to pass a value from a UICollectionView Item to a UIButton in Swift 4

我有一个UICollectionViewCell 此Cell在另一个UICollectionViewCell中注册,该UICollectionViewCell又在称为FilterCollectionViewControllerUIViewController viewDidLoad中注册。 当选择第一个单元格时,应将String类型的值传递给UIButton 此UIButton位于FilterCollectionViewController

FilterCollectionViewController我创建了一个名为colorName的变量。

var colorName: String!{
    didSet {
        print(colorName)
    }
}

在同一类中,有一个UIButton

@objc func addButtonFunc(sender:UIButton){
    print(colorName)
    }

FilterCollectionViewController我声明一个UICollectionView 在这个collectionView中,有一个“第二” UICollectionView ,它具有以下功能:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let filterVC = FilterCollectionViewController()
    filterVC.colorName = "blau"    
}

当从第二个UICollectionView单击一个单元格时,我可以从didSet (在主FilterCollectionViewController )打印colorName值。 但是,我希望将其传递给UIButton 单击此UIButton时,应进一步处理此字符串。 但是print方法结果nil

如果您位于同一类中,则不应创建FilterCollectionViewController的新实例,否则其他情况看起来很好,

创建一个像

var colorName: String {
    didSet {
        print(colorName)
    }
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    colorName = "blau"  
}

然后在addButton动作中

@objc func addButton(sender: UIButton){
    print(colorName)
}

首先,您必须转到情节提要,并将标识符 “ yourViewId”设置为

FilterCollectionViewController

然后

用这种方法

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let viewController = self.storyboard.instantiateViewController(withIdentifier:"yourViewId") as! FilterCollectionViewController
viewController.colorName = "red"
self.present(viewController, sender:nil)
}

我找到了问题的答案。

在父FilterCollectionViewController我将变量colorName

var colorName: String!

在第二个UIVieControllerCell我声明了Parent ViewController类的变量

var colorName: FilterCollectionViewController?

因此,在第二个UICollectionView中的函数didSelectItemsAt我添加了以下代码

colorName?.colorName = "blau"

必须在第一个UICollectionview ,第二个UICollectionView的父UICollectionViewFilterCollectionViewController的子级中再次声明它。 声明在函数cellForItemAt

cell.colorName = self

就是这样。 单击按钮时,颜色名称将更新。

这是缩写代码:

class FilterCollectionViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

var colorName: String!

override func viewDidLoad() {
        super.viewDidLoad()
//...register UICollectionview declare button etc.
}

 @objc func addButtonFunc(sender:UIButton){  
    print(colorName)
}

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellID", for: indexPath) as! FilterCell

        cell.colorName = self

        return cell
    }

在FilterCell类中:

var colorName: FilterCollectionViewController?
//Class is initialised with  function, in the function another UUICollectionView is registered.
//...functions like numberOfItemsInSection and cellForItemAt are declared

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    colorName?.colorName = "blau"

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM