ios - 从另一个类访问 IBOutlet

标签 ios swift2 xcode7 iboutlet

我有 2 个 swift 文件,一个是我的 HomeViewController,第二个是我的 EventCollectionViewCell。在第二个中,我有一个 IBOutlet = informationView :UIView,我想从 HomeViewController 访问它。

有什么办法吗? 先感谢您, KS

最佳答案

好吧,我想在 HomeViewController 中你有一个 UICollectionView 并且充当它的数据源,然后在你的 collectionView 数据源中你可以:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cellIdentifier", forIndexPath: indexPath) as! EventCollectionViewCell

    //Here you can access the IBOulets define in your cell
    cell.informationView.backgroundColor = UIColor.greenColor()

    return cell
}

编辑:

问题:当您点击一个单元格时,您想要在单元格内显示一个叠加层。

解决方法: 您需要数据模型来维护状态,无论它是否处于事件状态(informationView),假设 ItemCell 是我的模型(在您的情况下 Event 可以是):

class ItemCell{
    var active:Bool = false
}

collectionView:cellForRowAtIndexPath: 中,您将检查模型的当前状态,并以此为基础显示或隐藏叠加层:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cellIdentifier", forIndexPath: indexPath) as! EventCollectionViewCell

    let event = self.data[indexPath.row]
    //Here you can access the IBOulets define in your cell
    cell.informationView.hidden = !event.active

    return cell
}

然后作为最后一步,每次选择一个单元格(func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) 方法)时,您将更新模型的状态:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){
    let event = self.data[indexPath.row]
    event.active = !event.active
    collectionView.reloadData()
}

示例项目: https://github.com/Abreu0101/SampleTap

关于ios - 从另一个类访问 IBOutlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38104978/

相关文章:

ios - 有人可以解释 'CoreAnimation: timed out fence' 日志消息吗?

swift 2 Func 默认类型参数

swift 2 : Invalid conversion from throwing function of type to non-throwing function

swift - 如何检查 Swift 中的两组是否相同?

ios - Swift 2.0 中的“advancedBy”API

ios - 如果使用桥接头,Xcode 7 不会自动完成

ios - Appium 检查员无法找到集成的 applePay 屏幕

iphone - 来自 ios webview 的 Cookie

iphone - 连续/重复 MKMapView

ios - 无法在 XCode 7 beta 2 中调试