问题
I have a UIViewController in a tab bar application. I've added the controller from the MainWindow.nib file (i.e. not programatically).
My question is how can I make my view controller a singleton? (To resolve the Facebook delegate issue).
回答1:
You probably want to make your "Facebook connection code" a singleton (or part of the app delegate), but not the view controller itself. Then just wire up the FB thing with any view controller that needs it.
回答2:
If you really want to create singletons (but I don't think you do, please rethink your design - what is "the facebook delegate problem" exactly?), look here in Apple's sample code
回答3:
You can make any class a singleton adding something like this to the .m file (and you also have to add declaration to the .h):
+ (id)sharedInstance {
static id sharedInstance;
@synchronized(self) {
if (!sharedInstance)
sharedInstance = [[ClassName alloc] init];
return sharedInstance;
}
}
来源:https://stackoverflow.com/questions/5781749/uiviewcontroller-as-a-singleton