UIViewController as a singleton

♀尐吖头ヾ 提交于 2019-12-24 00:16:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!