I would know how loadNibNamed
of NSBundle
class works; In some document I find something like
[[NSBundle mainBundle] loadNibNamed:@"mynib" owner:self options:NULL];
without return value; just called inside a method (e.g. cellForRowAtIndexPath
if I want to customize my cell). In other documents I find:
NSArray* vett=[[NSBundle mainBundle] loadNibNamed:@"mynib" owner:self options:NULL];
In this case, for example, in cellForRowAtIndexPath
, I could
return [vett lastObject];
or something like this. The latter method seems to me clear; I load the nib in a vector and then I use the vector elements. The problem is understanding what exactly do the first:
[[NSBundle mainBundle] loadNibNamed:@"mynib" owner:self options:NULL];
no return value, no cell reference...where are the objects of my nib? how are they handled? I don't understand how it works
For example, you have a subclass UIView with custom nib @"CustomView"
You can load it:
NSArray * arr =[[NSBundle mainBundle] loadNibNamed:@"CustomView" owner:self options:nil];
CustomView * customView = [arr firstObject];
This method returns an array of the objects in the nib. If you want to instantiate a custom view for example you will want to use the return value in the way anthu describes.
NSArray * arr =[[NSBundle mainBundle] loadNibNamed:@"CustomView" owner:nil options:nil];
CustomView * customView = [arr firstObject];
If however you want to use the xib to configure the file's owner (note that you can pass in an owner to this method), you may not be interested in the array that is returned. E.g. If the xib connects the file's owner's IBActions and IBOutlets to elements in the xib.
[[NSBundle mainBundle] loadNibNamed:@"mynib" owner:self options:nil];
You may also combine both approaches.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With