views:

990

answers:

3

I have a large number of UIViews that are created in the app delegate applicationDidFinishLaunching and added to an NSMutableArray. The user pages through these views using a page control and scroll view. I created the UIView and its child components in IB. They are wired to a controller. To create 170 views on the iPhone takes about 30 seconds. Eventually, I'll have over 1000 views. Besides being slow, it will also kill the app because of memory use. How much faster and memory efficient is creating everything programmatically without views? How might some of the 6000 fact type apps be doing it?

Is there a better approach instead of creating everything at once? The user may access a view in slot # 400 and begin scrolling from there. Any suggestions are appreciated.

+2  A: 

It is difficult to suggest an answer without knowing a little bit more about your specific problem, but I would venture to say that if you expect to display 1000 different things, creating 1000 individual views in IB is not the way to go.

If your pages share a common layout, you can use a UITableView to display the content of each page, and store only the data for each page in your NSMutableArray.

An excellent tutorial on how to use UITableView can be found here.

If they do not share a common layout, laying things out programmatically will be the way to go. This should be no more memory or processor intensive than doing it using IB, and in fact it will probably be faster, since it removes the need to read and parse an XML file (which is what .NIB files actually are).

e.James
+5  A: 

UIViewControllers are lazy. They only load when requested, and automatically unload when memory is tight (and it's easy to unload them manually by calling self.view=nil).

"Load" here means "read a NIB," or "build programmatically." ViewControllers don't really care. Building programmatically can be a little faster since you don't have to hit the disk, but this is seldom the bottleneck, since you only display one view controller at a time.

As to which way to go, it's more style than performance (with the exception of UITableViewCells, which there are reasons you need to build programatically in most cases).

Start by studying the View Controller Programming Guide. It'll show you how iPhone intends you to do this.

To eJames' comment about NIBs being XML files, this may be slightly misleading. NIBs are binary files generated by compiling XIB files which are XML. I'd do actually profiling on the phone before assuming that NIB load time is actually a problem. Much as I am by nature inclined to programatic layout, I've found in practice that NIBs greatly simplify many UI issues in practice, and I always come back to them for large projects.

Rob Napier
A: 

(cannot comment so I ask here)

Did you come up with a solution to this? I also have a scrollView with "only" 40 UIViewControllers inside, each with a 5-row UITableView, a couple of labels and a 320x480 background UIImageView. I load only 3 at a time (current, previous, next) but the device hangs for about half a second (maybe less but enough to not be smooth) every time I swap pages. I would like a behavior similar to the Weather app and am currently stumbled.

mga
Just off the top of my head, you may want to have one UIViewController, with a UIScrollView inside, which houses the UITableViews.Are you using UIPageControl? You should.
mahboudz
It is kind of like a card game. Each card loads in a UIViewController that loas a NIB with the UITableView and other elements. It works fine and all. But I want to create a UIScrollView where I can view all the cards side by side. There's 40 cards. So I just instantiate the UIViewController that works elsewhere in the game (I don't want to re-create the whole thing by code just for the UIScrollView) but I think this continous loading of NIBs may be the problem. I also tried only loading the image but it still "lags" a little so I am not sure what's wrong. Thanks
mga
I finally had it run using a very low res version of the images and not using a NIB. Kind of how the Photos app works. When the scroller stops it loads the complete view. There is minimal lag now.
mga