Paul
Paul

Reputation: 43

Custom View (XIB) and IBAction

I created a custom view (xib). This view is nothing more than an elaborate button (e.g. contains a label and icon). I want to handle click events exactly like the standard UIButton. That is, control-drag the button to the IBAction function that'll be called when button is clicked. From the storyboard, I want to be able to control-drag my custom UIView to the IBAction function that will handle the event someone taps my view. I don't want use UITapGestureRecognizer if I don't have to.

Upvotes: 1

Views: 1089

Answers (2)

Vollan
Vollan

Reputation: 1915

Create a custom class for that Xib and then you use

class customButton: UIButton {

    @IBOutlet weak var textLabel: UILabel!
    @IBOutlet weak var imageView: UIImageView!
    override init(frame: CGRect, style: UITableViewStyle) {
            super.init(frame: frame, style: style)
            setup()
        }

        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
            setup()
        }

        func setup() {
              //Setup texts and images
        }
}

Now you can use this class as customClass in your Xib and in turn use it as a customClass on the button you wish to use it on. Next, just drag an @IBAction as you wanted to. And you can reuse this button with other texts and images for other classes if you create an configure function inside that you can call whenever you want :).

Upvotes: 0

livtay
livtay

Reputation: 573

Just include a UIButton in your custom view. You can customize the button so that it has no words or design, and then you can place your label and icon, etc. on top of that button. Then you will be able to ctrl-drag from your button to an IBAction function as you want

Upvotes: 0

Related Questions