3

I am trying to make my button, when tapped, to push to a new View Controller. I've tried many different ways but it won't trigger the function that I have it linked to. I also checked the 3D stack view of my layers and the button is on top and clickable, even when I check the background color, it's not being covered by anything else.

Does anyone have any ideas to what I am doing wrong?

For now I am trying to make the button print out the sentence in the console, however whenever I press it, the string doesn't pop up, so I haven't bothered to connect it to the view controller yet.

Also, I am coding this app without storyboards.

Here is my code below.

It is under the MainPageCell class declared as a UICollectionViewCell

private let playButton: UIButton = {
    let button = UIButton()
    button.setTitle("", for: .normal)
    button.backgroundColor = .clear
    button.translatesAutoresizingMaskIntoConstraints = false
    button.addTarget(self, action: #selector(MainPageCell.buttonTapped), for: .touchUpInside)
    return button
}()

@objc func buttonTapped() {
    print("I PRESSED THE BUTTON")
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
syds
  • 322
  • 3
  • 12
  • The duplicate is about doing this in a table view but doing it in a collection view is identical once you tweak the specific APIs being used. – rmaddy Apr 13 '19 at 23:18
  • thank you, but besides the segue part, my first question is why the button isn't even working properly when pressed, it won't print out the print statement – syds Apr 13 '19 at 23:36
  • Oops. Reopened. – rmaddy Apr 13 '19 at 23:38
  • Could you add more details? Are you trying to add a button to inside collection view cells? – Ahmet Alsan Apr 13 '19 at 23:42

2 Answers2

4

This line is wrong:

button.addTarget(self, action: #selector(MainPageCell.buttonTapped), for: .touchUpInside)

You cannot assign self as the action target in a property declaration initializer, because the instance designated by self does not exist yet. There is no error or warning (I regard that as a bug), but the action method is never called.

Move that assignment elsewhere and rewrite it, like this:

self.playButton.addTarget(self, action: #selector(MainPageCell.buttonTapped), for: .touchUpInside)
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Thank you! This worked! Just to clarify, I shouldn't have to put the action target in the playButton property because the whole button hasn't been initialized yet? Rather, say I put it in the viewDidLoad, or in this case I put the assignment in a override init.... block, this way, when the button is loaded, it the target will initialize because there is actually a button to work with. I think i understand what I did wrong here! – syds Apr 14 '19 at 01:11
  • OMG, I spent at least an hour debugging this problem. TY! – livingtech Jul 22 '20 at 23:01
  • 1
    @livingtech Yeah, it's frustrating isn't it? Like I say, I regard this as a bug. At least the compiler should warn you, "Hey, this doesn't do what you think it does!" – matt Jul 22 '20 at 23:17
0

Maybe try defining your button action under the UIView Class, I've had a problem like that before, only worked when i linked it to the View Class, Good luck

Crashie
  • 19
  • 6