how many times to I need to load a nib?
Not sure if this is a silly question, but how many times do I need to load any specific nib?
My understanding is that you are effectively unarchiving the nib when you load it, so is it available anytime afterward for initializing?
Example:
contr开发者_开发技巧olsViewController *controls = [[controlsViewController alloc] initWithNibName:@"controlsViewController" bundle:nil];
[self addSubview:controls.view];
Then, later I release this viewcontroller:
[controls release];
The next time I wanted to initialize this viewcontroller type, do I use the nib or just "init??
Spadict, There are a few things wrong here. Firstly, i care about trivial nitpicking less than most people, but it reeeeeally helps to get the language right. Really it does. Lets go slowly..
If you have a class 'ControlsViewController' (note the uppercase C) you can use it whenever you like. You can instantiate as many instances as you like, whenever you like. Reading the documentation for UIViewController (I'm assuming here that ControlsViewController is a class that you wrote and that it is a subclass of UIViewController) tells us that the designated initializer is -initWithNibName:bundle: . These means that usually we will use it like so:-
ControlsViewController *controls = [[ControlsViewController alloc] initWithNibName:@"aNib" bundle:nil];
Another way to create an instance of a class is using Interface Builder. In Interface Builder you can create as many instances of any Class you like, tweak their properties, then save them as is, configured just as you like, to a nib file. This is a good way to create your GUI instances, your windows, buttons, views, etc. because you can visually manipulate positions and sizes, and you can add a button to a view, or a view to a window by dragging and dropping - which is easier than coding.
A .nib file is comparable to a .zip file. We don't really care about the .nib file - we care about the instances of objects that we archived inside it. We are not unarchiving the .nib file, we are unarchiving the objects we created with Interface Builder and then archived as a nib file.
Everytime we load the nib file we get recreate the objects inside it, as they were when we saved the nib. As an example:- in Interface Builder i create a Window and i set it's background Colour to black and it's width to be 600px. I save it to a nib (there is one window object in this nib - but there is no limit to how many objects or what type).
When i run the app i load the .nib 5 times. I now have 5 Windows with black backgrounds that are 600px wide.
UIViewController's designated initializer takes the name of a nib as it's first argument. When we create an instance of UIViewController it will call '-loadView' to load the nib that we pass in, unarchiving the objects within. You never need to call '-loadview' and using a nib is in no way an alternative to this method.
So, a view can be created programatically, or created with Interface Builder and then loaded from the nib. Neither of these "involves drawing the view programatically". Once you have a view you can tell it to draw, and you might do this 50 times a second or you might do it once, but that is a separate operation.
I think I've figured this out so I'll try and answer my own question.
You need to load the nib every time you initialize a view controller that want to use it. Loading a nib is an alternative to the UIViewController's 'loadView' method which involves drawing the view programatically.
The nib isn't a object class and it's not something initialized into memory like an NSObject. It's simply an archived alternative to loading a view programatically and needs to be loaded every time you want to use it with a view controller.
Can anyone confirm or deny?
The Nib file is just a set of user interface objects that have been serialized. When you do [controller initWithNibName:@"file"]
the objects are rebuilt into memory from the file. The File's Owner from Interface Builder is a stand in for the controller
object being sent the initWithNibName:
message. So whatever connections you made from File's Owner to the view in Interface builder are connected to the controller
object when the nib is loaded. At the least this will probably be the view
property of UIViewController
.
The upshot of all this is when you do [controller release]
the controller
object will also release all it's references which include those objects loaded from the nib. So yes you will need to reload the nib because all those objects were deallocated.
A word of warning. I see you are doing [self addSubview:controls.view]
. If you are not doing something like [controller.view removeFromSuperview]
before you release the controller then the base view is still holding references to everything. (At least I think that's true.)
精彩评论