Chapter 18 Listing 18.10 IBAction private func moodSelectionChanged(_ sender:) not called when image button tapped

Hi, I keyed Listing 18.10 and other listings into Xcode, and configured Main.storyboard as instructed by the book, but when I ran the project in the simulator and tapped on those emoji images at the bottom of the screen, the addMood button didn’t have any changed.

I put a print(“button tapped”) statement in the @IBAction private fun moodSelectionChanged(_ sender: ImageSelector) method and ran the project again. When I tapped on any of those emoji images, nothing was printed in the console. That shows the moodSelectionChanged(_ sender: ImageSelector) method was not called when any of the emoji images (buttons) was tapped.

The logic here in the listing code leaves me wondering what the real sender is. Apparently, it should be the UIButtons instead of the ImageSelector. When I tap on one of the images, the code should respond to my tap. In this process, the underlying image selector view does receive any changes. That’s why the moodSelectionChanged(_ sender: ImageSelector) method doesn’t get called in response.

So, I wonder whether there is anything wrong with the code here.

Thank you!

I don’t have the book, but did you check in IB to make sure that method moodSelectionChanged is connected to an action?

1 Like

Thank you, ibex10, for your replay. You’re right.

I later found that I had failed to connect the moodSelectionChanged method to the buttonTapped event. The book does mention that such a connection should be made in the buttonTapped method in another class by invoking the sendAction(for:) method. But unfortunately I apparently overlooked that short section in the book.

Thanks again for your help.