Objective C: loadNibNamed method: how does it work?

I know how the loadNibNamed class NSBundle ; In some document, I find something like

 [[NSBundle mainBundle] loadNibNamed:@"mynib" owner:self options:NULL]; 

no return value; just called inside the method (e.g. cellForRowAtIndexPath if I want to customize my cell). In other docs 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 that. The latter method seems clear to me; I load a thread into a vector and then I use vector elements. The problem is understanding what the first one does:

 [[NSBundle mainBundle] loadNibNamed:@"mynib" owner:self options:NULL]; 

no return value, cell reference ... where are the objects of my nib? How are they processed? I do not understand how this works.

+5
source share
2 answers

For example, you have a subclass of UIView with a custom nib @ "CustomView"

You can download it:

  NSArray * arr =[[NSBundle mainBundle] loadNibNamed:@"CustomView" owner:self options:nil]; CustomView * customView = [arr firstObject]; 
+5
source

This method returns an array of objects in nib. If you want to instantiate a custom view, for example, you will want to use the return value in the way anthu describes it.

 NSArray * arr =[[NSBundle mainBundle] loadNibNamed:@"CustomView" owner:nil options:nil]; CustomView * customView = [arr firstObject]; 

If you want to use xib to configure the owner of the file (note that you can pass this method to the owner), you may not like the returned array. For instance. If xib binds the owner of the IBActions and IBOutlets file to elements in xib.

 [[NSBundle mainBundle] loadNibNamed:@"mynib" owner:self options:nil]; 

You can also combine both approaches.

+3
source

All Articles