ios - 如何在不继承的情况下访问另一个类的属性

标签 ios swift

我正在寻找一个小问题的解决方案, 我已经在单元格类中声明了一个按钮:

class MyCell: UICollectionViewCell {
    @IBOutlet weak var button: UIButton!
}

但现在我试图从我的主类访问按钮变量,如果加载 View ,按钮应该隐藏,并且在几个函数中按钮应该再次突出显示:

class mainClassController: UICollectionViewController, UICollectionViewDelegateFlowLayout {

    // here I try to create reference to the button variable
    ...
}

最佳答案

我想这应该可行(仅在 Playground 上测试)

class MyCell: UICollectionViewCell {
    @IBOutlet var button: UIButton!
}

var myCell = MyCell()   //Generate one object of your class, just put this outside any class

class main: UICollectionViewController, UICollectionViewDelegateFlowLayout{
    var cellButton = myCell.button
}

关于ios - 如何在不继承的情况下访问另一个类的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35994684/

相关文章:

ios - Apple Test Flight 如何上传低版本IPA?

iphone - 使用自定义形状的孔创建图层蒙版

ios - Alamofire:具有额外属性的可编码对象

ios - UICollectionViewCell 的 SelectedBackgroundView 在不应该可见时可见

ios - 创建可滚动的 UIView 时遇到问题(swift + Interface Builder)

swift 3 : issue with AVVideoCompositionCoreAnimationTool to add watermark on video

ios - 无法转换AnyObject的表达式类型?输入 '$T3'

ios - XCode 中的钥匙串(keychain)警告

ios - 修改 UITableView 中的 JSON 数组

ios - 解码自定义初始化中的所有属性(枚举类的所有属性并为它们赋值)