Can Someone Explain This Code?

Discussion in 'Mac Programming' started by Monergist, Apr 5, 2011.

  1. Monergist macrumors newbie

    Dec 22, 2010
    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?

  2. goMac macrumors 603

    Apr 15, 2004
    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.
  3. admanimal macrumors 68040

    Apr 22, 2005
    IBOutlet is just a macro that Xcode/Interface Builder uses to determine which variables can act as outlets.
  4. Sydde macrumors 68020


    Aug 17, 2009
    "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".
  5. Monergist thread starter macrumors newbie

    Dec 22, 2010
    Thank you! That was very helpful.

    Um, yeah. If I wasn't confused, I wouldn't have asked the question. ;)
  6. wlh99, Apr 5, 2011
    Last edited: Apr 5, 2011

    wlh99 macrumors 6502

    Feb 7, 2008
    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.
  7. Monergist thread starter macrumors newbie

    Dec 22, 2010
    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.

Share This Page