我有 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()
}
关于ios - 从另一个类访问 IBOutlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38104978/