• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Newbie: Referencing Objects
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Newbie: Referencing Objects


  • Subject: Re: Newbie: Referencing Objects
  • From: Ken Thomases <email@hidden>
  • Date: Sun, 26 Oct 2008 16:02:16 -0500

On Oct 26, 2008, at 3:21 PM, Nathan Kinsinger wrote:

On Oct 26, 2008, at 2:05 PM, john fogg wrote:

Hi again!

Thank you everybody for your answers.

On Sun, Oct 26, 2008 at 7:45 PM, Andy Lee <email@hidden> wrote:

I don't know Actionscript but it looks like it has a global dictionary of
objects that you can reference by name. There is nothing like "_root" in
Objective-C.

I see, this is where I was wrong. Yes Actionscript has this global dictionary. If you know the name of an object and its place in the hierarchy you can access it.

In my game I think of having a sort of "controller object" that
controls the whole state of the game. So I'd like to access it from
almost every other object further down the hierarchy. How would you do
this?


Is there a way to create the controller object and store its reference
in a global variable? Or is there another common way to do this that
I'm not thinking of?
Sorry for asking such dumb questions but I'm still new to this.


Thanks again in advance!

(not a dumb question)

You want to use the singelton pattern, see:
http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/chapter_3_section_10.html

Well, you don't necessarily need a singleton object or the global variable that you (John) are considering.


You should familiarize yourself with the Model-View-Controller (MVC) design pattern that's fundamental to Cocoa. A button would usually only talk directly to its controller. The controller is then responsible for knowing what other object(s) within its sphere of control needs to be messaged. If you feel that some other object outside of the immediate controller's sphere needs to be messaged, then you should forward the message up to a higher-level controller which controls the wider sphere.

Each controller should be connected to the objects which it controls. This is often done by connecting up outlets in nibs. Even when dealing with a view hierarchy, it's more common for the controller to have an outlet to a control deep in the hierarchy than it is for it to navigate the hierarchy starting from the top.

An important note: the names you give to objects in a nib are only cosmetic and are only meaningful when editing the nib. You can't access those objects by name in your code.


Typically, you'll have an application controller object. It will be an instance of a custom subclass of NSObject. Often it will be the application delegate, too.


This application controller will be the central organizer of your application. It will typically have references to your other application objects -- model objects, documents and/or document controllers, windows and/or window controllers, etc. If it doesn't have a direct reference to one of these, it probably has an indirect reference. That is, it has a reference to an object which has a reference to the desired thing.

Also, each of those objects usually has a path back to the application controller. If the application controller is the application delegate, then that's easy: [NSApp delegate] gets you the reference to the object.

Window controllers often act as the "File's Owner" for nibs which contain a window. You hook up outlets from the window controller to the window being controlled and, if desired, to specific views/ controls within the window.


Using some combination of the above patterns, you will usually find that's its straightforward to get a reference to the object you need.


Does that help?

Cheers,
Ken

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Newbie: Referencing Objects
      • From: "john fogg" <email@hidden>
References: 
 >Newbie: Referencing Objects (From: "john fogg" <email@hidden>)
 >Re: Newbie: Referencing Objects (From: Andy Lee <email@hidden>)
 >Re: Newbie: Referencing Objects (From: "john fogg" <email@hidden>)
 >Re: Newbie: Referencing Objects (From: Nathan Kinsinger <email@hidden>)

  • Prev by Date: Re: Newbie: Referencing Objects (second try)
  • Next by Date: Re: Newbie: Referencing Objects
  • Previous by thread: Re: Newbie: Referencing Objects
  • Next by thread: Re: Newbie: Referencing Objects
  • Index(es):
    • Date
    • Thread