从另一个类访问IBOutlet

时间:2016-06-29 16:11:47

标签: ios swift2 xcode7 iboutlet

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

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

3 个答案:

答案 0 :(得分:0)

嗯,我想在HomeViewController中你有一个UICollectionView并作为它的数据源,然后在你的collectionView dataSource中你可以做:

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
}

<强> 编辑:

问题:当您点击要在单元格内显示叠加的单元格时。

解决方案: 您需要数据模型来维护状态,如果它处于活动状态(信息视图),假设ItemCell是我的模型(在您的情况下可以是事件):

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

答案 1 :(得分:0)

按照Jose的回答以及您是否在界面构建器中创建单元格。将Identity Inspector中的单元格类设置为EventCollectionViewCell,并将单元格标识符设置为Jose指定的“cellIdentifier”。

答案 2 :(得分:0)

声明该单元格的属性并通过该对象访问IbOutlets。

@property(非原子,强)MSSearchHeaderView * searchHeaderView;

if(self.searchHeaderView.searchTextField.text.length&lt; = 0){

        self.searchHeaderView.clearButton.hidden = YES;

}否则{

        self.searchHeaderView.clearButton.hidden = NO;

}