开发者

Does adding a NSObject into MainMenu.xib creates a singleton object?

开发者 https://www.devze.com 2023-04-08 21:32 出处:网络
Let say I have a NSObject开发者_如何学C 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 obje

Let say I have a NSObject开发者_如何学C 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号