2

I want to create a custom action connection in a class, which should be visible in Interface Builder. For example - I add action / target properties to NSView class just like this:

weak open var object: AnyObject?    
open var something: Selector?

The action is something and the target is object. Now I want in Interface Builder to have 'Send Action' link / connection available for something and to be able to make connection to a @IBAction method to some class (for example the controller of the view), just like it can be done for a simple NSButton. Maybe this is not possible, or maybe I must add some keywords in front of the custom action / target pair, the same way we need to make a property @IBInspectable to appear in Attributes Inspector.

Any help is welcome ;-)

Duncan C
  • 128,072
  • 22
  • 173
  • 272
Ivaylo Nikolov
  • 501
  • 4
  • 13
  • you can add gesture to your view in story board and then connect this gesture to your @IBAction – canister_exister Dec 21 '18 at 15:57
  • Create a subclass of `NSControl` instead of `NSView`? – Willeke Dec 22 '18 at 11:26
  • @Willeke Thank you for your comment. What you are suggesting is a basic functionality of IB and it really doesn't matter which class I'll use if it is based on `NSControl`. It is the same with `NSButton`. The problem is that I do not want to use `NSControl` and I'm asking for a solution which can be used with any class. Thank you for the help - I must say that your comment is closest to the answer I'm looking for ;-) Anyway I believe that the functionality I'm searching is not available yet, but will give the post a little bit more time - one never knows ;-) – Ivaylo Nikolov Dec 28 '18 at 09:25

2 Answers2

0

I don't think you can do that. You can add @IBInspectable properties to custom NSView subclasses, but there is no selector or action data type for properties. The only allowed types are Boolean, Number, String, Localized String, Point, Size, Rect, Range, Color, Image, Nil.

Interface Builder has hard-wired support for handling the target-action properties of controls.

(BTW you should probably add the Mac OS tag to your quesiton. Most Apple traffic on this board is iOS-related.

EDIT:

As somebody said in a comment, if you're creating an object that has a target/action, it should probably be an NSControl, not an NSView. Controls are the object family that handle target-actions, and there IS a mechanism in Interface Builder for adding target/actions to controls.

Community
  • 1
  • 1
Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • I've added the macOS tag, but the question is not OS specific. – Ivaylo Nikolov Dec 22 '18 at 09:51
  • You're asking about `NSButton`, which is specific to Mac OS. There is an equivalent, `UIButton`, for iOS & TVOS, but `NSButton` is unique to Mac OS – Duncan C Dec 22 '18 at 13:15
  • Making a connection of target / action in Interface Builder is the same for macOS, tvOS and iOS. It just doesn't matter which is the OS. You always drag and create connection for action or outlet. And if there is a way to create custom connection in the way we can add properties via `@IBInspectable`, it will be again the same for all OS - just like `@IBInspectable` implementation is the same for all OS. Anyway thank you for the comment ... – Ivaylo Nikolov Dec 26 '18 at 09:47
  • I am well aware that Interface builder works the same way for iOS and Mac OS. (I've been developing for Macs since the 1980s.) The problem you will face in trying to create a custom IBAction is that Interface Builder only allows a very small list of data types to be inspectable, and IBAction is not one of the supported types. – Duncan C Dec 26 '18 at 16:13
  • Yes - you are correct. I suspected this but decided to ask anyway. I was hoping that someone with more experience (like yourself) to help me with some ideas how to achieve this or similar functionality. Now the question is with correct tag tho' ... – Ivaylo Nikolov Dec 26 '18 at 17:52
  • (I tried, and there is an IBInspectable tag here on SO. I added it to your question.) Also see the edit to my answer. – Duncan C Dec 26 '18 at 18:45
  • Thank you for the suggestion, but this is not what I'm looking for. – Ivaylo Nikolov Dec 28 '18 at 09:27
0

In iOS, there is one somewhat cheesy option -- you could have an outlet to a UIBarButtonItem, and someone could then drop a standalone UIBarButtonItem in their xib or storyboard. The UIBarButtonItem can then have the target/action, and your class could then pull the target/action from there in awakeFromNib and assign the values to your own class. That approach may be replicable in MacOS using NSToolbarItem, but I have not tried. It's a kludgy option really, as it requires extra xib objects and may not be too obvious what is going on, but it can work. Basically, the UIBarButtonItem / NSToolbarItem would only be acting as a container for the target/action values.

If you can't subclass NSControl, it would seem the preferred option is to have a delegate, probably. That is usually fine, unless one controller needs to be the delegate for multiple instances of your class, in which case the delegate methods get more tedious. Another option is to have your class have a block property, which is performed upon the action, but blocks must be assigned in code and not set up in Interface Builder.

Carl Lindberg
  • 2,902
  • 18
  • 22