2

OK, this may sound very basic (especially for someone who has written tens of thousands of Objective-C code), but I've always tried to avoid all this... or just tweak existing solutions. The result? I've never learnt how to do something simple like that.

So, here's my ultra-simple scenario:

  • I want to create a custom NSView (let's say a simple view with an image and a text in it), which I'll be able to assign in the Interface Builder (take an NSView element and set its class to MYCustomView - that's all - nothing more complicated)

  • I know I can write an NSView subclass and have it draw all my elements programmatically in drawRect: and all this - but I most definitely don't find any point in that.

  • What I do want is to simply draw the view in the Interface Builder (in our example, with a "placeholder" image and textfield), be able to use it as the "basis" of our NSView subclass, and also maintain pointers to the two elements in the view so that I can programmatically access them.

I know it's doable - I'm not asking about that. What I need is an ultra-simple walkthrough. Is there anything you can point me to?

Rephrasing the question in a... one-liner:

How can I replace the programmatic approach (seen in like 99.9% of NSView subclasses) in drawRect:, with a layout taken from a XIB?


P.S.

(A) Trust me, it must have been the 100th time I've been reading about NSViewControllers and all these, but not having used them, probably means that I still haven't found the point in using them...

(B) Please, don't shoot me with "what have you tried" questions. In the course of time, I've tried loads of things and at times I've somehow made it. However, it always feels like a crappy, messed up thing I just managed to get working. Nothing more, nothing less. All I want is to know if there is a simple tutorial on the above simple scenario.

(C) If I get an actual explanatory answer to this one, I guarantee I'll re-post it myself. You simply can't believe how many seasoned Cocoa developers have serious trouble dealing with this...

Dr.Kameleon
  • 22,532
  • 20
  • 115
  • 223
  • @JoeBlow a) Because, even though I'm interested in OSX and not iOS, I don't think it makes any difference in that particular case, does it? (OK, it's an `UIView` vs `NSView` thing, but is it really that different as a concept?) b) Because, as sad as it makes me, if a Cocoa-related question is perceived as OSX-only, it's like 100 times more likely than noone will ever notice it (while the iOS community is vastly larger...) :-) – Dr.Kameleon Oct 14 '14 at 07:37
  • 1
    If you want to do custom drawing then you *have* to subclass `drawRect` **or** use layers, and basically do the same thing inside the layers. `drawRect` isn't for layout - it's for drawing geometric shapes or whatever that cannot implemented using a Cocoa control. – Droppy Oct 14 '14 at 07:54
  • @Droppy Well, the thing is I'm not talking about any custom drawing. I simply want a re-usable specific view layout. The image will be at the very same position all the time. The textfield, too. It's just that I want to use them in like X different places, and still be able to access the inner elements of the view (set them programmatically, bind them, etc). That's all. – Dr.Kameleon Oct 14 '14 at 07:57
  • 1
    OK that's cool; but you need to remove references to `drawRect` in your question in order to avoid confusing people. – Droppy Oct 14 '14 at 07:57
  • @Droppy Probably I used it because - hmm hmmm - I'm kinda confused myself? :) – Dr.Kameleon Oct 14 '14 at 07:59
  • @JoeBlow Hmm... I did, only for that Storyboard vs XIB part. Sorry. – Dr.Kameleon Oct 14 '14 at 08:17

1 Answers1

2

I've always wanted "custom" Storyboard classes as well!

This may not totally answer your question but this is just how we do it now, in iOS: just use container views.

Full extremely long tutorial: https://stackoverflow.com/a/23403979/294884

Everything's a container view in iOS now.

What we do is just have a scene, and then duplicate it: then change the colors or whatever as you describe.

Here's a literal example from the storyboard that was open behind this browser window!

enter image description here

Notice the small class/scene thing, we just copy it. Notice in the example it is slightly customised, just as you say. They are all the same class (it happens to be caled "BookBist") {"bist" == "bouncy list" btw}

Then as I say container views are the secret because, well, it's for exactly this purpose, it's why apple finally introduced "container views".

(BTW on that long container view tutorial. Search down to What if you want (say) a table controller or a page view controller instead of a UIViewController? it's a critical trick when making container views! Ridiculously Apple gives you a "default" VC when you drag in a container view; of course you never want that; in the example at hand I put the small BookBist scenes connected to the container views wherever they are needed.) Example...

enter image description here

Now, I 10000% understand what you are asking and have always wanted to know the answer myself!

For use HALF the answer, is, as I say, "copy the scene" so that works perfectly in modern storyboard. I appreciate that sucks, because what you want is a prefab, like in any game engine such as Unity3D, right? Me too!

But do note that THE OTHER HALF of your answer is certainly "container view magic" - "everything's" a container view now in iOS, indeed Apple finally put them in to make a rational way to do exactly the sort of thing you describe.

halfer
  • 19,824
  • 17
  • 99
  • 186
Fattie
  • 27,874
  • 70
  • 431
  • 719
  • Whoa. That's quite an answer. Thanks a lot. Give me some time to go through it! :-) – Dr.Kameleon Oct 14 '14 at 08:12
  • if you think that's long, try my answer I linked to !! :) really "container views" are the new way to go with, everything, in storyboard. it's completely fantastic -- and solves some if not all issues – Fattie Oct 14 '14 at 08:13
  • Are container views available in OSX storyboard (10.10) ? – GoodSp33d Oct 14 '14 at 08:26