UICollectionView在Header(Swift)中添加动作

时间:2015-02-10 10:54:20

标签: swift uicollectionview uisegmentedcontrol

我有一个collectionView和该集合视图的标题。我在我的标题中添加了一个UISegmentControl,现在我正在尝试将selectedSegmentIndex值传递给main collectionView,所以我试图在我的UICollectionViewController中为该段添加目标,但它没有返回任何内容,这里是我的代码

 override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {

        let headerView = self.collectionView?.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "header", forIndexPath: indexPath) as collectionHeaderView
        headerView.controlSegment.userInteractionEnabled = true
        headerView.controlSegment.addTarget(self, action: "segmentAction:", forControlEvents: UIControlEvents.TouchUpInside)
        return headerView
    }

    func segmentAction(sender: UISegmentedControl) {
        println(sender.selectedSegmentIndex)
    }

1 个答案:

答案 0 :(得分:1)

正确的控件事件为.ValueChanged

headerView.controlSegment.addTarget(self, 
        action: Selector("segmentAction:"), forControlEvents: .ValueChanged)