Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Apr 5, 2011, 08:46 PM   #1
Monergist
macrumors newbie
 
Join Date: Dec 2010
Can Someone Explain This Code?

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!
Monergist is offline   0 Reply With Quote
Old Apr 5, 2011, 09:25 PM   #2
goMac
macrumors 603
 
Join Date: Apr 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.
goMac is offline   0 Reply With Quote
Old Apr 5, 2011, 09:53 PM   #3
admanimal
macrumors 68040
 
Join Date: Apr 2005
IBOutlet is just a macro that Xcode/Interface Builder uses to determine which variables can act as outlets.
admanimal is offline   0 Reply With Quote
Old Apr 5, 2011, 09:55 PM   #4
Sydde
macrumors 68000
 
Sydde's Avatar
 
Join Date: Aug 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".
__________________
You got to be a spirit. You can't be no ghost.
Sydde is offline   0 Reply With Quote
Old Apr 5, 2011, 10:08 PM   #5
Monergist
Thread Starter
macrumors newbie
 
Join Date: Dec 2010
Quote:
Originally Posted by goMac View Post
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.

Quote:
Originally Posted by goMac View Post
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.
Monergist is offline   0 Reply With Quote
Old Apr 5, 2011, 10:38 PM   #6
wlh99
macrumors 6502
 
Join Date: Feb 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.
__________________
Warren Holybee

Last edited by wlh99; Apr 5, 2011 at 10:45 PM.
wlh99 is offline   0 Reply With Quote
Old Apr 5, 2011, 10:52 PM   #7
Monergist
Thread Starter
macrumors newbie
 
Join Date: Dec 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.
Monergist is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Library for simple Face and QR Code/Bar Code/Etc detection DarthFoley iPhone/iPad Programming 0 Jan 26, 2014 12:11 PM
Resolved: ML code for OSX server code Processor OS X 10.8 Mountain Lion 5 Jul 27, 2012 11:51 PM
AppStore:"The code you entered is not recognized as valid code" (or Already Redeemed) sevimli OS X 10.8 Mountain Lion 223 Jul 27, 2012 04:50 AM

Forum Jump

All times are GMT -5. The time now is 05:15 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC