vburojevic
vburojevic

Reputation: 1666

When to use multiple nibs?

Let's say my application has 6 windows, 1 windows is main windows, and other 5 are for settings, scores, statistics etc.

So if I'm correct, multiple nibs are used because of memory managament? So If I put all 6 views in one nib file, when loading application it will load all 6 views at the same time and take a lot of memory, but if I use 6 nibs for 6 views, on startup application will load only first view, and when I click in exammple "Options" then it will load Options.nib and diplay the view.

Are there more reasons to use multiple nibs instead of multiple views in one nib?

Am I right? And how do I know when to use addSubview or presentModalViewController? What is the main difference when I'm using those two methods to switch views?

Upvotes: 3

Views: 311

Answers (2)

Moshe
Moshe

Reputation: 58067

Nib (or XIB) files have almost nothing to do with memory management. They are there for convenience purposes. It's easier to lay out UI and wire up actions with a visual tool than hand coding all of the autoresizing masks and actions. However, you are correct, you don't want to load all of your views into memory at once as that would be a waste.

Using multiple XIB files is useful for keeping your project more structured. (You might even have alternate nibs for a single view controller in some cases. Although the common case is a 1:1 ratio.)

The addSubview and presentModalViewController methods are really different. addSubview takes a view and adds it to a given view (or brings it to the forefront). This means that you would be responsible for retaining the view controller so that odd things don't happen. This would be used for presenting other views in your main view, such as presenting a message in a banner or the like.

By contrast, presentModalViewController is meant for presenting a single view for a short amount of time, such as a log in form, or an informational screen. You pass in the entire view controller piecemeal and often will release the view controller shortly thereafter. In fact, it's common to see code like this:

SomeViewControllerSubclass *myVC = [[SomeViewControllerSubclass alloc] initWithNibName:@"SomeViewControllerSubclass" bundle:nil];
[self presentViewController:myVC];
[myVC];

Notice how we release the instance of SomeViewControllerSubclass immediately after presenting it. You can't do that with addSubview, because then you may be left with a view without it's controller. You have to retain it yourself.

Upvotes: 1

taskinoor
taskinoor

Reputation: 46027

  1. You are right about memory. All the views of a nib are loaded simultaneously in memory. You may not need them at once. So in that case you are wasting memory. This can even crash the app if you have many views.

  2. Adding separate nibs in general lead to more structured code. There is no logical relation between settings and score. So you should have separate classes and nibs for them. If you don't do that, you may have an unmanageable code which will require more time in maintaining and modifying.

Upvotes: 1

Related Questions