How can I create an `IBOutlet` or` IBAction` in a custom class

how can I create an IBOutlet or IBAction subject to the following conditions:

  • IBAction or IBOutlet must be created inside my custom class (inherited from NSObject )
  • NSButton instance visible inside Interface Builder (XCode 4)

In short, I would like to add a click action from a button to my class.

Any help appriciated, thanks :)

EDIT 02/19/2012

I asked questions without providing all the necessary data ... I just needed to read a few more cocoa manuals ...

I wanted to connect programs to components from other areas, which is impossible from what I know :(

+4
source share
2 answers

Look at the Apple Cocoa design documentation that comes with Xcode 4, which is pretty fundamental to the Xcode / Cocoa / interface designer model.

In the scheme:

 @interface MyCustomClass - (IBAction) myButtonClickAction:(id)sender; @end @implementation MyCustomClass - (IBAction) myButtonClickAction:(id)sender { NSLog(@"My button has been clicked"); } @end 

Now in the interface designer (just open the .xib file in Xcode to get the constructor) you need:

  • Add an instance of MyCustomClass - select an object from the library of objects and drag it onto your objects (usually on lhs of canvas) or onto the design canvas (it just goes to the objects and does not create a graphic widget on the canvas). Now select the added object and in the inspector (usually on rhs of canvas) set the class to MyCustomClass . Now, when your application starts, an instance of MyCustomClass will be created.
  • Select NSButton on the design canvas, select the Connections tab in the Inspector. Click and drag from the selector in the Sent Actions section to MyCustomClass in the Objects section. When released, you will get an IBAction menu to select, select myButtonClickAction .
  • You will probably want to add an IBOutlet to the delegate of your application to associate it with the instantiated custom object created, if you do not, you do not have direct access to it. The process for this follows the same pattern as for the IBAction above.

That is, in words (image help) and very briefly.

Now read these lessons!

+5
source

IBOutlet and IBAction are just flags that the interface designer will use to connect UIComponents and their actions to classes (hence the IB prefix). This is all they are used for - you do not use them to programmatically connect action buttons.

If you want to connect a button to an action, you need to add and activate the button.

(not sure about the syntax with NSButton, as I am an iOS programmer, but browsing through documents, and this should be clear)

0
source

Source: https://habr.com/ru/post/1392236/


All Articles