Reputation: 1057
There are similar posts about this topic, but my situation seems to be a little different. With that said, it also feels like it should be a common problem to overcome.
I have a view controller (StatsViewController) that has a blank UIScrollView at the moment. What I'd like to do is create a custom UIView class, and lay it out using Interface Builder and then have the StatsViewController use this view multiple times. But it's an unknown number of times currently. Basically, the StatsViewController will be pulling in some JSON from a web service, looping through the service, and for each "loop" I'd like to instantiate a new view using my custom UIView and add it to the UIScrollView within the StatsViewController.
My problem is when I create the UIView in interface builder, I can't figure out how to instantiate it using the nib through the loop. I read that you load view controllers using a nib, and not plain UIView's... but at the same time, I read that you can't (or shouldn't) have nested UIViewControllers, so I'm at a loss.
If I create a custom view in Interface Builder, how can I instantiate it multiple times on the same view controller?
Upvotes: 1
Views: 602
Reputation: 20236
A simple trick to get what you want is the same way you'd implement a UITableViewCell
in Interface Builder.
In Interface Builder, set the file owner to UIViewController, load your nib, and then grab that view controller's view. You can then (so long as you retain the view), get rid of the VC.
So what I suggest you do, is watch the first 2/3 of the WWDC10 session 104, to get an idea how to implement cell reuse. Then if you want to implement your cells in IB, use the above method (there are other ways to do it too, but they're all hackish), you can do that.
Upvotes: 1
Reputation: 2781
Each view will be it's own object. If you store each into an array, you can get to each view via the array. This would allow you to control them all with one controller.
And no, you load nib's with the nib loader. They might describe whatever. It doesn't need to have a controller in it.
Upvotes: 1