开发者

UIViewController as a singleton

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).


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.


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


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;
  }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜