0

Say I have an OSX app with some sprites moving around, call them gremlins. When I click on a gremlin, I want to display some sliders that control some attributes of that gremlin, maybe scale, speed, direction, that sort of thing, then click the gremlin again to dismiss the sliders. Then click another gremlin and display the sliders to control that one's attributes. I can easily create some NSSliders and set them up in the code. But I'd much rather create a set of sliders in Interface Builder, and load them on-demand: if I wanted to rearrange them and play with their relative positions, it would be nice to do it using that visual interface, rather than having to fiddle with the code, recompile, run, click on a gremlin, and see how it looks. Plus it just seems like better design in general--but I don't know, I'm new to Apple Land, and haven't done anything professionally in this environment yet.

I've tried googling every variation I can think of on "Swift: programmatically load/connect to controls created in Interface Builder", but haven't found any demonstrations or discussion on how to do this, or even an indication that anyone does it. It's making me worry that this is just not the way things are done in Apple Land. Is this possible? Prohibitively/unnecessarily difficult? Is it what people do? If it is possible, what is it called, ie, what can I google for to find out more about it?

SaganRitual
  • 3,143
  • 2
  • 24
  • 40
  • 1
    I've never had to do anything like that, but it can definitely be done. Worst case scenario, a view controller, with a centred view in the middle (do need to give it a height and width but you can adjust them programmatically later). The outside layer with an alpha 0 and the centred one with a "clear" background colour. Then presenting it as a VC, whilst any touch outside your UI elements (via a delegate) can removeFromSuperview(). However, I am confident there's ways to use a "view", which would give you greater control and flexibility, but this is one way you can try. – Alex Ioja-Yang Aug 21 '17 at 21:03
  • 1
    https://stackoverflow.com/questions/30335089/reuse-a-uiview-xib-in-storyboard this is probably best approach for your needs – Alex Ioja-Yang Aug 21 '17 at 21:08
  • @AlexIoja-Yang That was exactly what I was looking for, thanks so much. If you like, post an answer saying something like "It's called _reusing a view xib_", and I'll mark it as the accepted answer. – SaganRitual Aug 22 '17 at 12:06
  • 1
    @AlexIoja-Yang Haha, I meant post it as a formal answer, so you can get the reputation points. – SaganRitual Aug 22 '17 at 12:11
  • 1
    it kept posting it as a comment on it's own. Glad to have helped – Alex Ioja-Yang Aug 22 '17 at 12:19

1 Answers1

0

Try having a look for "reusing a view xib".

This seems like a good starting point: Example

  1. Create xib file
  2. Design it in Interface Builder
  3. Create a custom UIView file and create initialisers for the view
  4. Add file as owner of the xib (Interface Builder)
  5. Connect elements from xib to Outlets
  6. Load the xib in the UIView file initialisers
  7. Assign it to new UIViews you create
Alex Ioja-Yang
  • 1,428
  • 13
  • 28