Is it better to embed a view controller or add a preview?

For example, if I need a UITableView appear under additional content in a view, it would be best:

  • add a UITableView as a subtitle or
  • add container containing UITableViewController ?

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

+4
source share
3 answers

The general rule for iOS, especially for the iPhone / iPod touch form factor, is that there should only be one view controller on the screen at any given time. The only case I can think of when you want to break this guide is when you have a subheading that has a lot of logic built into it, and the peep should be included in several other views. For example, popover should have its own view controller, because it may need to be shown in combination with several other view controllers.

In your case, I would strongly suggest just adding a UITableView as a subordinate if (1) there is a lot of logic in the table view that is not related to the logic of the parent view, or (2) you need to display the same kind of table in another part of your application .

+3
source

You have several options:

  • Add a UITableView and make the containing view controller a data source and table delegate.
  • Add a UITableView and make some other class a data source and table view delegate.
  • Create a separate view controller with a table, and then add a second view controller as the child view controller of the 1st.

The choice depends on the appropriate separation of responsibilities and encapsulation.

Options 2 and 3 allow you to reuse if you need to embed a table view in multiple view controllers.

There is no easy answer to your question. It all depends on your needs, your data and the structure of your application.

+5
source

I would prefer to use a container controller for the following reasons:

  • It can be reused in another part of the application.
  • It supports a single responsibility of objects (the main viewing controller focuses on displaying information about the model, and the table view controller processes the display of communication information in the model, for example).
  • Keeps source files smaller and easier to read.
  • Easier to create tests for.

Ultimately, they are both valid solutions.

+1
source

Source: https://habr.com/ru/post/1498991/


All Articles