0

I want to connect a custom protocol delegate from a storyboard instance of a custom class.

I found this similar issue from 2014 Can not connect custom protocol delegate from storyboard in Xcode 6.1

Is there a solution for this in the future (we are in the future. It is the year 2019 and cars can drive themselves).

So I have to set up an outlet with AnyObject

@IBOutlet weak var buttonStatusDelegate: AnyObject!

which can be set in the storyboard or through code i.e.:

testButton.buttonStatusDelegate = self

and then to call the delegate I have to cast to the type using the code

if let bsd = buttonStatusDelegate as? StatefulButtonStatusChanged { bsd.stateCallback(self.status)}

So is there a better way that can support a custom button in both the storyboard and in code, or is the AnyObject solution above the only solution?

stevenpcurtis
  • 1,907
  • 3
  • 21
  • 47
  • The goal of an outlet is to connect an object *physically* and to initialize the connected object. A protocol has no initializer and what do you connect the protocol to? By the way in 2019 *swifty* closures are preferred over *objective-c-ish* protocol/delegate – vadian Apr 17 '19 at 06:38
  • There's kind of the same issue with closures. How can you use a closure when the custom class is created in the storyboard? You'd surely have to set....an @IBOutlet... – stevenpcurtis Apr 17 '19 at 07:01

0 Answers0