How to add a custom UIControl class in storyboard?
Asked Answered
B

2

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?

Betteann answered 5/9, 2014 at 23:23 Comment(0)
A
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.

Ambiguity answered 5/9, 2014 at 23:28 Comment(5)
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?Betteann
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 storyboardBetteann
@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.Ambiguity
I'm not sure what you mean by "load the specific view". The view will be loaded automatically as part of the storyboard unarchiving.Ambiguity
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: https://mcmap.net/q/504292/-where-to-initialize-target-in-a-uibutton-particularly-caring-for-ibdesignableJackfruit
R
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 .

Rubdown answered 26/6, 2018 at 7:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.