Does Interface Builder's new IBDesignable attribute work with NSButton and other controls in Cocoa?
Asked Answered
W

2

13

I have managed to get the IBDesignable/IBInspectable attributes working with direct subclasses of NSView but not with a direct subclass of NSButton. This is causing me to question if in fact the Cocoa implementation is somehow limited to NSView only.

Almost every example on the web (and Apple WWDC 2014 Xcode video) use NSView and then drag a custom view component from the library onto the canvas (and then change its class).

Is it possible to use IBDesignable with subclasses of NSControl and NSButton etc...? I have seen many examples on the web using UIButton.

If it is possible, then what are you supposed to drag from the library onto the canvas? It doesn't make sense for it to be a "custom view". On the other hand, there is no "custom control" available.

To be clear, I can get the IBInspectable attribute to show up at design time; but any changes don't seem to live render at design time.

The workaround is to wrap any custom NSButton I want to create within an NSView (via composition) but this seems like a bit of a hack...

Wharfinger answered 12/12, 2014 at 22:17 Comment(1)
Have you found some solution?Vanny
N
0

I started playing around with a custom NSButton and NSButtonCell.

Dragging a button from the library onto the canvas and changing its class and the cell class doesn't live render. I think this is because Interface Builder still does a lot of custom things to setup NSButtonCell.

What works fine for me is dragging a custom view from the library onto the canvas and set its class. For this to work you need to setup the cell inside NSButtons -initWithCoder:.

Also I found a sample from Apple with a layer-backed custom Checkbox.

Nissy answered 17/12, 2014 at 21:47 Comment(4)
Could you elaborate on "For this to work you need to setup the cell inside NSButtons -initWithCoder:" and why this works. Recent documentation from Apple says not to touch the buttonCell so I haven't paid much attention to "cell" topics. Thanks.Wharfinger
Does someone have this sample? I've got an error "Sorry, that page cannot be found." =(Vanny
@Nissy - the link is deadAbsalom
link is dead, tried the source from Github - sampe doesn't work anymore (tried checkbox.swift and it doesn't render if added as nsbutton to storyboard). Those who look for solution - it only works with custom NSViews dragged into xib/storyboard.Corder
C
0

You need to drag an NSButton onto the view, then set the Custom Class to your specific NSButton descendant. Not sure why it doesn't work when you start with an NSView.

What can give you a hint is that the NSButton specific attributes aren't in the "Attributes Inspector". Hence there must be some setup at the time you drag the control onto the view.

Caryloncaryn answered 5/6, 2018 at 18:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.