0

I'm following an iOS tutorial on Design Patterns from Ray Wenderlich's website and I'm currently reading through the area about singletons. The tutorial describes singletons as objects you'd only like one instance of so that only one instance can be referred to during the life of your application. Other tutorials I've worked through usually create a singleton for classes such as a ItemManager for a to-do list app therefore the idea of this concept does make sense. However I'm curious to know why aren't UITableViews created as singletons in most books/tutorials?

Laurence Wingo
  • 3,912
  • 7
  • 33
  • 61
  • 1
    TableView is a view, which you can add it as subview to a single controller's view, a view can only have a single superview, if you add to a new one, its removed from the previous one, it might also can create memory issue since singleton only will be dealloc when app get killed – Tj3n Mar 08 '18 at 02:51
  • How about you want use one custom tableview class in two different controllers. – NF Lee Mar 08 '18 at 02:51
  • An UI Element cannot be a singleton. Do you only want to allow one table per app ? :) this could help https://stackoverflow.com/questions/4074154/when-should-the-singleton-pattern-not-be-used-besides-the-obvious – Simon Franzen Mar 08 '18 at 02:52
  • @NFLee I'm not too clear on what you mean. Could I not use a custom tableView in two different controllers if the UITableView is a singleton? – Laurence Wingo Mar 08 '18 at 02:57
  • @SimonFranzen why can't a UI element be a singleton? I don't understand. – Laurence Wingo Mar 08 '18 at 02:58
  • 1
    Views are rarely (if ever) a singleton because you never have any need to share a single view throughout your application. There's simply no point. In your case, You would never have a singleton of a table view and then share that table view across multiple screens in your app. – rmaddy Mar 08 '18 at 03:04
  • @rmaddy Thank you for your help on this. Can I ask, what if it were a custom UIButton that just said "Submit". Would it make sense to make that type of button a singleton? – Laurence Wingo Mar 08 '18 at 03:16
  • 1
    Absolutely not. Views are not shared through the lifetime of your app across multiple view controllers. You might have a custom button class that makes it simple to create a "Submit" button. But it won't be a singleton. You would create a new instance of that custom button each time you needed one in your app. – rmaddy Mar 08 '18 at 03:18

1 Answers1

2

The Singleton pattern is meant to be used with a class that you never need more than one instance of during the entire lifetime of your app (hence the name).

Views do not fall into this pattern. A view class is not designed or meant to have a single instance that is shared all across the view controllers of your app or live during the lifetime of your app.

Views are meant to be created when needed and go away when no longer needed. There is no benefit to a singleton view instance.

UITableViewController is a prime example where a singleton makes no sense. You are never going to create just one table view instance that is used in many different view controllers. You simply create whatever instances you need when you need them.

If you have a more specialized view (such as the "Submit" button mentioned in comments), you still would not use a singleton. You would create a custom UIButton class and whenever you need such a button you create a new instance of this class.

Save the Singleton pattern for a class you really will never need more than a single instance that needs to live throughout the entire lifetime of your app as it runs.

Note that singletons tend to get overused and they actually have a lot of potential issues. Some searching (not specific to iOS) on the Singleton pattern will offer plenty of good reading on the subject.

rmaddy
  • 314,917
  • 42
  • 532
  • 579