mamaral / Onboard

An iOS framework to easily create a beautiful and engaging onboarding experience with only a few lines of code.
MIT License
6.46k stars 765 forks source link

Expose the button handler action as a property #47

Closed emaloney closed 9 years ago

emaloney commented 9 years ago

There's a Catch-22 with Swift constructors that makes it difficult to use the button handler.

Specifically, because the button handler was only settable from the constructor, it was not possible to use in cases where the button handler needed to reference the view controller itself.

It's probably hard to visualize, so imagine this scenario:

You want to subclass OnboardingContentViewController from Swift. Within your subclass's init, you attempt to call super as follows:

super.init(title: "Hi!", body: "How are you?", image: nil, buttonText: "Great!", action: {
    self.buttonTapped()
})

This scenario won't compile, because Swift won't allow you to use self from within init before super has been called. In this instance, the action block contains a reference to self; since the action block is a parameter to super.init(), the reference contained inside is illegal and results in build error.

By making it possible to set the button action handler after construction, it is now possible to achieve the desired result with:

super.init(title: "Hi!", body: "How are you?", image: nil, buttonText: "Great!", action: nil)
self.buttonActionHandler = { self.buttonTapped() }
mamaral commented 9 years ago

Great catch - thank you!