SwiftKickMobile / SwiftMessages

A very flexible message bar for UIKit and SwiftUI.
MIT License
7.3k stars 742 forks source link

Button Action Not working when using custom xib file #398

Closed annathomasQB closed 4 years ago

annathomasQB commented 4 years ago

When we add a button inside a custom xib file, its action doesn't seem to work. I did cross-check with the demo app and could see that the buttons were added inside a stack view. When I used the xib from the demo app, it worked. But even in the demo app, if I place a button inside the topmost content view(its called Background View in the demo app's TacoDialogView.xib), adding an action to this button still doesn't work.

I'm curious as to the implementation, because in my xib file I actually want a top right cross button to close the MessageView, but adding it to a stackView just makes it difficult to be placed at the top right corner.

Is there any specific reason why it's working only inside a stackView?

I would like to add that having SwiftMessages is such a great relief!! Thanks for such a great library! :)

wtmoose commented 4 years ago

I'm pretty sure you're not wiring your button up right. I attached an example of the taco dialog with an additional button that prints "Hi!" to the console when you tap it.

Add a second button demo

In this case there are two buttons, so I had to add another IBAction to TacoDialogView and wire it to the new button. However, if you only have one button, you shouldn't need to do that. MessageView already provides an IBOutlet for a button.

/// An optional button. This buttons' `.TouchUpInside` event will automatically
/// invoke the optional `buttonTapHandler`, but its fine to add other target
/// action handlers can be added.
@IBOutlet open var button: UIButton? {
    didSet {
        if let old = oldValue {
            old.removeTarget(self, action: #selector(MessageView.buttonTapped(_:)), for: .touchUpInside)
        }
        if let button = button {
            button.addTarget(self, action: #selector(MessageView.buttonTapped(_:)), for: .touchUpInside)
        }
    }
}

There's no IBAction for this button. Instead, you set the buttonTapHandler:

/// An optional button tap handler. The `button` is automatically
/// configured to call this tap handler on `.TouchUpInside`.
open var buttonTapHandler: ((_ button: UIButton) -> Void)?

The existing nib files already have a button that's wired up. So if that is your starting point, you can just reposition the existing button.

Your other option is to subclass MessageView, being sure to update the class of the top-level view in your nib file, and add any IBOutlets and IBConnections you need.

annathomasQB commented 4 years ago

Wow thanks @wtmoose for your response.. I'm pretty sure I had wired up the buttons correctly. But yea your demo works great !! 😄 And in my project I've actually taken the Taco.xib file an customised it because I wasn't able to get it working with a xib that I created :/ .. but it's working for now.. :D thanks for your response.. and I'll see if I can get a demo project where the issue came up, and send it over so that the issue if it occurs again, can be reviewed by you guys, and help others out if they have the same problem again.. thanks again @wtmoose .. I'm closing this issue now.