Skip navigation.
 
mlRe: Bypassing Interface Builder
FROM : Graham Cox
DATE : Thu May 15 05:43:27 2008

On 15 May 2008, at 12:54 pm, Johnny Lundy wrote:

> Just another note - the reason that I asked this is that I finally 
> added a second class to my project. I had never had 2 classes in a 
> project before because I didn't understand how to have them 
> communicate. Actually, I still don't - I just try everything until 
> something works.


If you mean this literally, I doubt that it's ever going to work. 
Software's like this: there are millions of ways for it to fail, and a 
handful of ways for it to work. The chances of stumbling across one of 
the working ways in the vast sea of possible non-working ways purely 
at random is negligible.

> I wrote the interface and implementation files, and the app would 
> not load. Of course, I had not instantiated the class because 
> although I knew that myInstance = [MyClass new]; would make an 
> instance, I did not know where to put this code. It seemed recursive 
> to put it in the implementation of itself.
>
> So I dragged out an NSObject in IB, named it the class name, and 
> everything worked. I still don't understand why. I presume it was 
> that dragging it out instantiated it. Or maybe having an -init 
> method instantiated it. Class browser doesn't show instances, so 
> can't check there.



Dragging it out caused it to exist, yes. A nib file contains objects. 
When the nib is loaded, those objects are attached to your program as 
real live instances.

> The reason I want a name for the instance is so I can use it as the 
> receiver when I send messages to it from another class's methods.


Right, but understand, the name of the object in IB is not this. It is 
nothing of any use to your code, nor does it mean anything 
significant. It's merely a name - rename it "donkey kong" if you want, 
it makes no difference.

In your code, you have, somewhere (in a suitable object):

IBOutlet id     myObjectInstance;


In IB, you connect (ctrl-drag) from "somewhere", myObjectInstance to 
the object it refers to. "Somewhere" could usefully be File's Owner, 
but since for MainMenu.nib this is NSApp(lication), you would need to 
be subclassing NSApplication to add the myObjectInstance outlet. Apart 
from being not a very good approach, I infer from your email that you 
wouldn't know how to do that anyway. Instead you might get further by 
using an application delegate, but you would still need to define this 
and write code for it before trying to connect to it in IB. 
Ultimately, to be useful, an object in IB MUST be connected to an 
outlet, or to another object that is connected to an outlet. If not, 
there is no way for your code to refer to it - it might exist in 
memory but it's an orphan.

To send a message to your object, elsewhere in your code, you can 
write [myObjectInstance doSomething];

Maybe you are expecting too much of Interface Builder. Its job is to 
build interfaces. It doesn't write any code, and it doesn't do 
anything magic. Maybe you'd be better off starting with writing some 
non-interface code so you can understand how objects are instantiated, 
how they are assigned to variables, and how you send messages to them. 
Once you get this, it should be clearer what IB can, and can't do for 
you.


hth,

G.

Related mailsAuthorDate
mlBypassing Interface Builder colo May 14, 16:36
mlRe: Bypassing Interface Builder I. Savant May 14, 17:11
mlRe: Bypassing Interface Builder colo May 14, 17:35
mlRe: Bypassing Interface Builder I. Savant May 14, 17:43
mlRe: Bypassing Interface Builder Andy Lee May 14, 17:50
mlRe: Bypassing Interface Builder colo May 14, 17:53
mlRe: Bypassing Interface Builder I. Savant May 14, 17:58
mlRe: Bypassing Interface Builder David Wilson May 14, 17:59
mlRe: Bypassing Interface Builder I. Savant May 14, 18:01
mlRe: Bypassing Interface Builder I. Savant May 14, 18:05
mlRe: Bypassing Interface Builder Gregory Weston May 14, 18:06
mlRe: Bypassing Interface Builder glenn andreas May 14, 18:13
mlRe: Bypassing Interface Builder Wayne Packard May 14, 18:26
mlRe: Bypassing Interface Builder Andy Lee May 14, 18:37
mlRe: Bypassing Interface Builder Stefan Werner May 14, 18:46
mlRe: Bypassing Interface Builder Bill Bumgarner May 14, 19:00
mlRe: Bypassing Interface Builder Boyd Collier May 14, 19:34
mlRe: Bypassing Interface Builder colo May 14, 19:37
mlRe: Bypassing Interface Builder Julius Guzy May 14, 22:05
mlRe: Bypassing Interface Builder Torsten Curdt May 14, 22:49
mlRe: Bypassing Interface Builder colo May 15, 00:53
mlRe: Bypassing Interface Builder Graham Cox May 15, 01:26
mlRe: Bypassing Interface Builder Graham Cox May 15, 01:41
mlRe: Bypassing Interface Builder colo May 15, 02:14
mlRe: Bypassing Interface Builder Johnny Lundy May 15, 02:15
mlRe: Bypassing Interface Builder Graham Cox May 15, 02:29
mlRe: Bypassing Interface Builder I. Savant May 15, 02:43
mlRe: Bypassing Interface Builder Graham Cox May 15, 02:45
mlRe: Bypassing Interface Builder I. Savant May 15, 02:51
mlRe: Bypassing Interface Builder I. Savant May 15, 02:54
mlRe: Bypassing Interface Builder Andy Lee May 15, 03:02
mlRe: Bypassing Interface Builder Graham Cox May 15, 03:02
mlRe: Bypassing Interface Builder Andy Lee May 15, 03:06
mlRe: Bypassing Interface Builder I. Savant May 15, 03:07
mlRe: Bypassing Interface Builder Andy Lee May 15, 03:08
mlRe: Bypassing Interface Builder I. Savant May 15, 03:09
mlRe: Bypassing Interface Builder I. Savant May 15, 03:15
mlRe: Bypassing Interface Builder Andy Lee May 15, 03:29
mlRe: Bypassing Interface Builder Julius Guzy May 15, 03:35
mlRe: Bypassing Interface Builder I. Savant May 15, 03:37
mlRe: Bypassing Interface Builder Johnny Lundy May 15, 04:33
mlRe: Bypassing Interface Builder Julius Guzy May 15, 04:36
mlRe: Bypassing Interface Builder Johnny Lundy May 15, 04:54
mlRe: Bypassing Interface Builder Graham Cox May 15, 05:43
mlRe: Bypassing Interface Builder Uli Kusterer May 15, 07:50
mlRe: Bypassing Interface Builder Uli Kusterer May 15, 07:55
mlRe: Bypassing Interface Builder Uli Kusterer May 15, 08:15
mlcocoa mentoring (was Re: Bypassing Interface Builder) Torsten Curdt May 15, 15:36
mlRe: cocoa mentoring (was Re: Bypassing Interface Builder) Paul Bruneau May 15, 15:47
mlRe: cocoa mentoring (was Re: Bypassing Interface Builder) Uli Kusterer May 15, 16:08
mlcocoaheads frankfurt (was Re: cocoa mentoring) Torsten Curdt May 15, 16:44
mlRe: cocoa mentoring (was Re: Bypassing Interface Builder) Torsten Curdt May 15, 16:51
mlRe: Bypassing Interface Builder Andy Lee May 15, 17:11
mlRe: Bypassing Interface Builder Johnny Lundy May 15, 18:40
mlRe: cocoa mentoring (was Re: Bypassing Interface Builder) Dennis Munsie May 15, 19:34
mlRe: Bypassing Interface Builder Paul Sargent May 15, 20:07
mlRe: Bypassing Interface Builder Uli Kusterer May 15, 20:48
mlRe: Bypassing Interface Builder Shawn Erickson May 15, 22:20
mlRe: Bypassing Interface Builder Shawn Erickson May 15, 22:30
mlRe: Bypassing Interface Builder Johnny Lundy May 15, 22:32
mlRe: cocoa mentoring (was Re: Bypassing Interface Builder) Torsten Curdt May 15, 23:03