Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Monergist

macrumors newbie
Original poster
Dec 22, 2010
16
0
I'm new to GUI programming on the Mac, and I ran across this little bit of code that absolutely helps me do what I want to do, but I'm not sure what's going on in it. Here's the code:

@interface ReadWriteController : NSObject {


IBOutlet NSTextField* pathField;
IBOutlet NSTextField* outputField;

}

So, I understand that there is an NSTextField pointer. I understand that I need an outlet to send IBActions. What I don't understand is what this specific syntax actually does. Is it specifying that the NSTextField has an IBOutlet of the same name?

Thanks!
 

goMac

Contributor
Apr 15, 2004
7,662
1,694
Same name? It's defining two IBOutlets. Both are of type NSTextField *. One is named pathField. The other is named outputField.

You also generally don't send IBActions. IBActions are sent to you.

It sounds like there is a fair bit of confusion here.
 

admanimal

macrumors 68040
Apr 22, 2005
3,531
2
IBOutlet is just a macro that Xcode/Interface Builder uses to determine which variables can act as outlets.
 

Sydde

macrumors 68030
Aug 17, 2009
2,552
7,050
IOKWARDI
"IB" stands for "Interface Builder". What this is for is to allow you to connect a NIB object to a program object so that when the program starts up, the correct outlet object will be properly connected to the controller object that needs to have access to it. For instance, if you need to change the name of a specific button, you need to have a reference (outlet pointer) to that specific button.

Apart from Interface Builder connections, "IBOutlet" is an otherwise meaningless specifier, and "IBAction" is equivalent to "void".
 

Monergist

macrumors newbie
Original poster
Dec 22, 2010
16
0
Same name? It's defining two IBOutlets. Both are of type NSTextField *. One is named pathField. The other is named outputField.

You also generally don't send IBActions. IBActions are sent to you.

Thank you! That was very helpful.

It sounds like there is a fair bit of confusion here.

Um, yeah. If I wasn't confused, I wouldn't have asked the question. ;)
 

wlh99

macrumors 6502
Feb 7, 2008
272
0
As you noticed and pointed out, pathField and outputField are pointers to NSTextField objects.

But where are those objects coming from? You might have also noticed that nowhere in your code do you alloc and init an NSTextField, they are magically just there, able to be used.

NIB files are files that contain objects, already created/init'ed and ready to be used. When the NIB file is loaded, so are those objects. IBOutlets tell the compiler that those pointers will be pointing to objects in a NIB file. The connections need to be made in interface builder so the compiler will know which object in the NIB file the pointer points to.

So without the IBOutlet, those pointers would point to nothing until your code created an object and assigned the pointers to point to the new object. With the IBOutlet, those pointers point to objects in the NIB file.

Also, IBActions are unrelated and not required for IBOutlets. ABActions work in the reverse. There are pointers in the objects in the NIB file. IBActions are methods that those pointers can point to. So, if an object in the NIB file needs to send a message to an object in your code, an IBAction will allow that connection to be made. The most common would be if a button is clicked, that button object (in the NIB file) sends a message to an IBAction. The pointer in the NIB file needs to point to that IBAction, and that is done by the connection in IB.

Hope that makes sense.
 
Last edited:

Monergist

macrumors newbie
Original poster
Dec 22, 2010
16
0
Wow Warren, thanks for the comprehensive explanation. That helps a lot. Like I said, my code works, but if I don't know why then I won't be able to do it on my own or troubleshoot should problems occur.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.