在另一个班级中使用一个班级的变量

时间:2018-06-25 10:38:11

标签: swift

我有两个班级:CollectionCellViewController

第一类用于Collection View Cell(在其中放置了一个按钮)。在CollectionCell类中,我需要为此按钮连接插座:@IBOutlet weak var firstPercent: UIButton!

ViewController类中,我需要使用以下按钮的变量:firstPercent

我该怎么做?感谢您的帮助。

4 个答案:

答案 0 :(得分:0)

collectionViewDelegate

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

现在声明您的自定义单元实例,然后

您可以在ViewController中使用CollectionCell.firstPercent addTarget并将其设置为 做任何你想做的事。

答案 1 :(得分:0)

您可以通过以下方式获得它。

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

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

    let keyValue = self.dataList[indexPath.row] as! [String:Any]
    cell.lblZoneName.text = keyValue[Keys.name] as? String

    if(keyValue[Keys.status] as? Bool)! {
        cell.lblZoneName.textColor  = UIColor.white
    } else {
        cell.lblZoneName.textColor = UIColor.red
    }
    return cell
}

让我们假设ZonesCell是包含lblZoneName作为标签的collectionCell。 您可以使用与标签相同的按钮。

答案 2 :(得分:0)

您可以为此使用委托。

为您的单元添加一个名为“ CollectionCellDelegate”的协议,并为您的按钮操作添加一个委托方法。将单元格本身作为该方法的参数传递。符合ViewController中的“ CollectionCellDelegate”并在ViewController中实现委托方法。最后但也是最重要的一步,从cellForItemAt方法将视图控制器分配为CollectionCellDelegate。

protocol CollectionCellDelegate : class {
    func didPressButtonFirstPercent(_ cell: CollectionCell)
}

//Inside your CollectionCell class declare a variable to hold your cell delegate

class CollectionCell: UICollectionViewCell {
    //Add a variable to hold your delegate
    weak var delegate: CollectionCellDelegate?

    //Outlet for your button
    @IBOutlet weak var firstPercent: UIButton!

    //Action for your button
    @IBAction func didPressButtonFirstPercent(_ sender: Any) {
         delegate?.didPressButtonFirstPercent(self)
    }
}

//ViewController Conform to CollectionCellDelegate

class ViewController: UIViewController, CollectionCellDelegate{


     func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: “Cell_Identifier”, for: indexPath) as!                           CollectionCell

        //Set ViewController as your CollectionCell delegate
        cell.delegate = self
        return cell
     }

    //Implement delegate method here

    func didPressButtonFirstPercent(_ cell: CollectionCell) {

        //You can access your button here like below
        Let firstPercent =  cell.firstPercent

    }
}

答案 3 :(得分:0)

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate{


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

        // get a reference to our storyboard cell
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! CollectionCell

        // Use the outlet in our custom class to get a reference to the UILabel in the cell
        cell. firstPercent.tag = indexPath.row
 example project

        return cell
    }
}