How can I add a target to buttons to a collectionview

I build an app for tvOS but somehow I'n not able to add a working target to a button within a collectionview.


Who can help me out? I think it has something to do with adding a subview. All radioStationTiles are presented. I can browse from left to right and back but pussing one of the buttons wont work. I looks like I can select it.


   override func viewDidLoad() {
        super.viewDidLoad()
    
        collectionView.delegate = self
        collectionView.dataSource = self
        
    }


public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
       
        return radioStations.count
       
    }
   
   
    public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
       
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "radioStation", for: indexPath) as! CollectionViewCell
       
        let radioStation = radioStations[indexPath.row]
       
        let radioStationName = radioStation[0]
        let radioStationSlogan = radioStation[1]
        let radioStationURL = radioStation[2]
        let radioStationTegel = radioStation[3]
       
        cell.radioButton.setBackgroundImage(UIImage(named: "tegelAchtergrondWhite (250x250).png"), for: UIControlState.normal)
        cell.radioButton.setBackgroundImage(UIImage(named: "tegelAchtergrondBlue (250x250).png"), for: UIControlState.highlighted)
        cell.radioButton.setBackgroundImage(UIImage(named: "tegelAchtergrondBlue (250x250).png"), for: UIControlState.focused)
        cell.radioButton.setBackgroundImage(UIImage(named: "tegelAchtergrondBlack (250x250).png"), for: UIControlState.selected)
       
        cell.radioButton.isEnabled = true
        cell.radioButton.isUserInteractionEnabled = true
        cell.radioButton.adjustsImageWhenHighlighted = true
       
        cell.radioButton.setImage(UIImage(named: radioStationTegel), for: UIControlState.normal)
       
        cell.radioButton.tag = indexPath.row
        cell.radioButton.addTarget(cell.radioButton, action: #selector(selectRadiostation(sender:)), for: UIControlEvents.touchUpInside)
     
        return cell
    }
   
    @objc func selectRadiostation(sender: UIButton)
    {
        /
        print("play")
    }

Accepted Reply

Seems a duplicate of your other thread today:


How can I add a target to buttons within a collectionview


If you're trying to move it, check the 'Actions' button up on the right?

Replies

Seems a duplicate of your other thread today:


How can I add a target to buttons within a collectionview


If you're trying to move it, check the 'Actions' button up on the right?