1

Let say I have a NSObject AppController:NSObject. Using IB, I drag an NSObject control into MainMenu.xib and points the class to AppController. Since MainMenu.xib is loaded once and objects inside MainMenu.xib are in memory for the life of the app, does it make the AppController object a singleton?

Then I can drag an IBOutlet to AppDelegate to access this singleton object. This looks like a quick way. Is this a good practice or to be discouraged?

The standard method I supposed is to add a static AppController *sharedInstance inside the class and use a +(AppController *)sharedAppController for access.

jemeshsu
  • 1,784
  • 16
  • 14

1 Answers1

1

No, it's not a singleton because nothing stops you from creating another instance of the same class in code.

It's just a convenient way to create a single instance.

and objects inside MainMenu.xib are in memory for the life of the app

This is not true. If nobody retains these objects (or holds a strong reference to them under GC), they will get deallocated. This is true. See Peter Hosey's comment below.

Ole Begemann
  • 135,006
  • 31
  • 278
  • 256
  • “This is not true. If nobody retains these objects (or holds a strong reference to them under GC), they will get deallocated.” On the Mac, this is not true. If nobody releases these objects (when not under GC), they will get deallocated. AppKit retains top-level objects on behalf of the File's Owner: http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/LoadingResources/CocoaNibs/CocoaNibs.html#//apple_ref/doc/uid/10000051i-CH4-SW6 MainMenu's File's Owner is the NSApplication object. On iOS, you are correct; all objects must be retained by something to stay alive. – Peter Hosey Sep 28 '11 at 23:11
  • Thanks for the correction, Peter. I have corrected my answer. – Ole Begemann Sep 28 '11 at 23:23