Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it better to embed a view controller or add a subview? [closed]

For example, if I required a UITableView to appear beneath additional content in a view, would it be best practice to either:

  • add a UITableView as a subview, or
  • add a container view holding a UITableViewController?

As far as I know, both would provide the same user experience, however I am interested to know which method would be considered best practice?

like image 639
Andrew Sula Avatar asked Dec 25 '22 21:12

Andrew Sula


2 Answers

You have several options:

  1. Add a UITableView and make the containing view controller the table view's data source and delegate.
  2. Add a UITableView and make some other class the table view's data source and delegate.
  3. Create a separate view controller with the table and then add the 2nd view controller as a child view controller of 1st.

The choice depends on the appropriate division of responsibility and encapsulation.

Options 2 & 3 allow for reuse if you ever need to embed the table view in more than one view controller.

There is no simple answer to your question. It all depends on your needs, your data, and your app's structure.

like image 78
rmaddy Avatar answered Apr 26 '23 04:04

rmaddy


The general rule for iOS, especially for the iPhone/iPod touch form factor, is that you should have only one view controller on the screen at any given time. The only case I can think of when you would want to violate that guideline is when you have a subview that has a lot of logic built in to it, and the subview needs to be included in multiple other views. For example, a popover should have its own view controller because it might need to be shown in conjunction with multiple other view controllers.

In your case, I would strongly suggest just adding the UITableView as a subview unless (1) the table view has a lot of logic that isn’t related to that of the parent view controller, or (2) you will need to display the same table view in another part of your app.

like image 22
bdesham Avatar answered Apr 26 '23 04:04

bdesham