28

I have a custom UIControl class that I want to use in my storyboard. I am having problems since there isn't a UIControl in the object library in the Storyboard console.

I tried adding a UIView and assigning my custom UIControl class to it, but it seems to not register the events that I need for the control to use(Value Changed, Touch Drag Inside, Touch Up Inside).

When debugging, I breakpointed the actions related to the events and it looks like they are not even called.

How can I use my custom UIControl class correctly in my storyboard?

3254523
  • 3,016
  • 7
  • 29
  • 43

2 Answers2

37

You are doing it correctly -- drop a UIView onto your storyboard and assign the class in the Identity Inspector. Don't forget to override initWithCoder:aDecoder in the custom class.

memmons
  • 40,222
  • 21
  • 149
  • 183
  • hey thanks for responding. just making sure that i'm doing this right can you confirm i'm doing this step right? - go to Identity Inspector and change the class to my custom UIControl Class. And after that... What would I be doing in `initWithDecoder:aDecoder` to point to that storyboard object? – 3254523 Sep 05 '14 at 23:34
  • I've done something similar when dealing with a .xib file, but since this is on storyboard, i'm not sure how to load the specific view, and if theres any kind of ID i need to assign it within storyboard – 3254523 Sep 05 '14 at 23:37
  • @christopher.ryan.cruz In `initWithDecoder` you'd be doing anything you'd normally do in `initWithFrame` since `initWithFrame` won't be called on a view dropped into a storyboard. – memmons Sep 05 '14 at 23:47
  • I'm not sure what you mean by "load the specific view". The view will be loaded automatically as part of the storyboard unarchiving. – memmons Sep 05 '14 at 23:48
  • Thanks for the great answer. Note, you don't, absolutely, nor necessarily, have to use `initWithDecoder`. Firstly, you don't necessarily need any particularly setup; don't use it if not needed. Secondly the **pair** `required init?(coder decoder: NSCoder)` / `override init(frame:CGRect)` is useful in Swift. Thirdly `awakeFromNib` can be a good choice (assuming you use storyboard normally). Unfortunately IBDesignable becomes involved; with custom controls you always use that. it's a tricky issue! Some discussion: http://stackoverflow.com/a/37444957/294884 – Fattie Jun 04 '16 at 18:29
0

Ok, So As i have figured out recently , Whenever we need to use Custom Controls through some 3rd party library, We must do:- Take example of TwicketSegmentedControl UIControl class that need to be added to your project through Interface Builder , Then:-

1. Drag UIView to your Design file 
2. Replace default  'class name' in Identity Inspector of this Control from UIView to 
   TwicketSegmentedControl

3.(Important step) Also Assign 'Module Name' below 'class name'  in Identity Inspector to TwicketSegmentedControl.

That's it. Done.

Now access it by using IBOulet in your file.

Also,for any further issues, Check if -initWithCoder called in Library file if need to debug the code .

Vinod Supnekar
  • 143
  • 1
  • 7