UICollectionView + UIButton in CollectionViewCell - "didSelectItem" don't trigger

hi :slight_smile: i am trying to create a sort of a game menu - i have created a CollectionView in “EntryViewController” and via segue want to pass the data to “SecondViewController”

the content of “textSent” would be used in SecondViewController for setup of the game

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    textSent = String(indexPath.row)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
    if segue.destination is SecondViewController
    {
        let vc = segue.destination as? SecondViewController
        vc?.username = textSent
    }
}

anyway, the UIButtons that are used to switch to “SecondViewController” (the action is setup in main.storyboard) do not pass the data, because “didSelectItem” does not trigger

is there some kind of simple solution to this?

of course i might create a lot of buttons and set the action to each button separately, but it is not a solution - i want to have “free hands” - dont know how many buttons i would need :slight_smile:

are you sure it did not trigger?, print putting a print statement and see if it fires in the debugger

if it does then how did you pass this value to your secondviewcontroller?

see video :slight_smile:

i created a label down on the EntryViewCentroller

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    
    textSent = String(indexPath.row)
    label.text = String(indexPath.row)
        
}

the variable textSent is set up only if i click the image, not the button… the button switches to SecondViewController only :confused:

hmm how is your button set up? it seems that its not part of your collectionview

i have created a CollectionView, in MenuItem imageView+Button

the connection between the two ViewControllers is setup with ctrl+click on Button und Blue Line was stretched on SecondVC

hmm, i think instead of a direct segue for the button you need to put the segue call inside the uibutton press, then set the textSent there (just before you call the segue)

based on your code i don’t see an IBAction attached to the button, so thats what im referring to, make it do an IBAction first, then call the segue push from there

well :slight_smile:

i have managed to set the segue to the Cell, BUT:

after click on the Cell, it calls the method prepare first:

override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
    print("first")
    if segue.destination is SecondViewController
    {
        let vc = segue.destination as? SecondViewController
        vc?.textRecieved = textSent
    }
}

where the textSent is naturally empty :slight_smile: and after that it calls the method “didSelectItem”

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    print("second")
    textSent = String(indexPath.row)
}

where i finally set the value to textSent, but it is unfortunately too late :slight_smile:

i have tested this with debug and printing “first” and “second” and it is definitely done in this order…

i have tried to add a button to set the value textSent “manually” and after that to switch - it works… meaning the segue is working :slight_smile: