Jean Le Moignan Jean Le Moignan - 1 month ago 8
iOS Question

Creating a UIView subclass that can trigger segues

Instances of UIButton, UITableViewCell, etc can be used as segue sources in Interface Builder (i.e. you can ctrl-click those objects and link them to controllers in a storyboard).

Can we create a direct UIView subclass that allows the same functionality? If so, how?

NOTE: Here's another way to ask my question: how can I add items in the "Triggered Segues" section of my own classes? Here's what one can see for a UITableViewCell:

Triggered Segue

Answer Source

As I liked to have the same functionality as Jean, I was puzzled. Disclaimer: I'm a total noob on iOS and Apple, so I assume it must be me and my lack of understanding why it's not working.

This 'answer' is more a theory, and maybe a very bad one. So please let me know if there' already a solution.

Theory

I do not think it is possible to extend/control the 'Triggered Segues' section via code alone. It seems like XCode knows based on the control chosen from the Object Library which Triggered Segues to display.

enter image description here

Thoughts

Having only a subclass of e.g. UIButton did not get me the Triggered Segues Section in the Connections Inspector. Setting the class in the Custom Class section does not help either.

enter image description here

Only when I use a Button from the Object Library, I get the Triggered Segues Section. E.g. if I subclass from UIButton, I should use a 'Button'.

enter image description here

I played around with the XML in my Main.storyboard - whenever I changed a <subviews><button ... to a <subviews><view ... the 'Triggered Segues' section is gone. Change it back and it appears. See below. I changed the element which contains the customClass="IconButton" attribute from button to view.

Button: Having Triggered Segues

<viewController title="..." id="Zzf-WE-vxI" customClass="MyCustomClass" ... >
    <view key="view" contentMode="scaleToFill" id="Mfn-O2-CMJ">
        <!-- ... -->
        <subviews>
            <button customClass="IconButton" customModule="...">
                <!-- ... -->
            </button>

View: No Triggered Segues

<viewController title="..." id="Zzf-WE-vxI" customClass="MyCustomClass" ... >
    <view key="view" contentMode="scaleToFill" id="Mfn-O2-CMJ">
        <!-- ... -->
        <subviews>
            <view customClass="IconButton" customModule="...">
                <!-- ... -->
            </view>

Conclusion

My conclusion is that XCode shows Triggered Segues section based on the element/control in a storyboard's XML - it is not dependent on the class I subclass from. So I doubt it's possible to control the contents of the Triggered Segues section by code.

Please let me know if I'm wrong. As said, I've got no real experience with the internals of XCode right now and I'm curious if my theory is just bla bla.