0

Often, when I'm making my apps, I'm in this situation : I have a UINavigationController, handling the view stack, some UIViewControllers, controlling their respective views...

But when I want to add several custom UIViews in my mainView, I don't know how to manage my code.

Each UIViewController needs to handle one and only one view (wich normally occupy all the screen size), and a view should not control their content (update it a the extrême limit). You can't neither do this :

[myViewController1.view addSubview:childViewController.view];

So if I want to achieve something like this, what should I do ? My Mockup

The orange parts have to be 3 instances of the same UIView(Controller?), but with a content depending of a NSObject (User, obviously).

I think this very important to segment your content, this should be an easy problem, but I found a lot of contradictory answers so, what's the best practice to handle this common issue? Theses orange views should be instances of UIViewControllers in order for it to handle their UITableViewDatasource? Is addChildViewController relevant in this case?

I already found a lot of things which work, but I don't know what should I do... Also, I'm using xibs.

Thanks in advance if you can help me (and other people I think).

4 Answers 4

1

You can do it either way (view or view controller) depending on how you want to handle things. Certainly, you can have one object be the data source for multiple tables, so in that case, you would just add multiple views. If, however, you want to keep your code more compartmentalized, then add view controllers, and have each control its own view -- to do this, you do need to use addChildViewController, and use the methods that Apple describes for creating custom container controllers. Alternatively, you can use container views in a storyboard which makes the process of creating custom container controllers simpler.

Sign up to request clarification or add additional context in comments.

3 Comments

I will test it, as it appears to be the cleaner way. But I'm afraid it will be complicated for nothing. Am I the only one facing this issue? Thank you anyways.
Don't see how multiple instances of a view, or viewcontroller, is an 'issue'.. It's actually much cleaner code if you can define a viewcontroller that handles all aspects of view and 'controller' aspects of things, it's area of concern is just it's single user, and then some parent view controller can add as many of those as it wants, formatting them however.
The implementation and the organisation of code is the issue, but it seems like you were right : your solution seems to be the cleanest, and the way things should do. Before iOS 5 I did manage the hierarchy with UIViews manually transformed to viewControllers, but it was like dirty. But now with addChildViewController, problem solved!
0

You're on the right path... Create separate instances of your subviews, and add them to your view. If you will have more than 3 (for instance, imagine coverview for your music, and you could scroll indefinitely left and right), I'd take a look at UICollectionViewController ... That will help manage cell re-use.

But, if it's just 3, just create three instances with different frames and add them to your view.

3 Comments

UICollectionView is not flexible enough, but 3 was just an arbitrary number... Also, what should be the type of the subview in question? UIView or UIViewController in order to manage the UITableView inside?
Why isn't UICollectionView flexible enough? (And, I'd opt to using a custom UIViewController subclass)
Because for example if I want something like this it does not work : Another example of silly mockup
0

Here's how I'd do it:

each orange box will be a custom view (inherits from UIView)

the view will have the label, image and the tableview.

since you are not sure of the number of instances of these views you'd be using, its better to use some kind of tagging, so that you can have one place for the datasource and delegate methods of the tables in these orange views.

in the datasource and the delegate methods, you can make use of the tableView.tag (same as the orangeView.tag property).

I personally dislike having more than one viewController in a view (except the splitVC), probably because I haven't had a such requirement.

I dont see how a uiviewcontroller for orange box would help, over a uiview.

as @James Boutcher mentioned in his answer, UICollectionViews will simplify this issue further.

2 Comments

Hey thanks, but I've no problem to handling several tableViews in several files, or only one file. The problem is that a UIView (or UICollectionViewCell) should not be the datasource / delegate of a tableView, and a UIViewController has to be unique (or almost).
hmmmm, you are right about not having the delegate/datasource in the view class. You can have it in the viewController, and assign the view controller as delegate and datasource for these tables.
0

Why not creating a UIView class and overriding the drawRect method and then adding subView for this class in your myViewController1.view

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.