djt February 2016

View Controllers and Multiple Views

I'm working on my first iOS app and am trying to understand something about Views and View Controllers.

My question is this: In an App like Instagram there's an 'Activity' tab in the main Tab Bar Controller. The activity tab has a UISegmentedControl for "Following" & "You" which display similar data, but query different records. Would the data in each of these UISegments be controlled by a separate controller? (FollowingController and YouController, for example).

What about instances like viewing locations on a Map, and you want to have a Map view and a List view? Would there be a MapViewController and a ListViewController, or would one View Controller manage all of this, and different views would be loaded conditionally based on what the user selects?

Answers


Fengson February 2016

No, you could use a single UIViewController and/or UITableView (depending on how you would like to present your data) and just change the dataSource based on Segmented Control's option.

For more information, please check UITableViewDataSource for instance. In case of Table View, dataSource is nothing else than an array of some data.

Another approach besides Table View / Collection View would be to put Container View in UIViewController which would use just a single UIViewController instance. For more information refer to the documentation. Especially, take a look at the picture:

Taken from Apple Documentation.

For your second question - no again. You can have a single UIViewController and place instances of MapKit and/or list (aka UITableView). You can do that in Storyboard by dragging an instance of MapKit like this:

enter image description here

And your single UIViewController might look like this:

enter image description here


Brian Ogden February 2016

Sounds like inheritance to the rescue here.

If you have a UISegmentedControl for "Following" and another UISegmentedControl for "You" then it there is the possibility that the best architecture will be to use inheritance and have two separate controllers for each of those UISegmentedControls, with names FollowingSegmentedViewController and YouSegmentedViewController, this way your separate controllers know the type of data they are loading and working with ("You" data or "Following" data) and other special considerations or differences between your "You" UIView and your "Following UIView like one being a map and one being a table.

And then for common UISegmentedControl functions and properties you have a base class that both your FollowingSegmentedViewController and YouSegmentedViewController inherit from, call it BaseSegmentedViewController or whatever is appropriate for your app and archiecture.

This inheritance allows you to share common UI elements (via @outlets) and other methods/functions between FollowingSegmentedViewController and YouSegmentedViewController but then also address differences between the two specifically in their distinct controllers.

Post Status

Asked in February 2016
Viewed 2,166 times
Voted 10
Answered 2 times

Search




Leave an answer