2

When we use the loadNibNamed method to get nib file, why will return a array, is not a nib file name corresponds to a nib file? I try to print this array's count, I found it always return 1. I hope you can help me to explain, thank you every much! Here is the snippet of code :

 NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"TestView" owner:self options:nil];
 NSLog(@"array's count is %ld",array.count);
fly_basket
  • 49
  • 7
  • Have you also testes xib files with more than one view inside? This is why this method returns array. a xib can contain more than one view. – regetskcob Mar 22 '16 at 07:48
  • 1
    oh, I see, thanks.I drag a ViewController to the xib file and found it return 2. :) – fly_basket Mar 22 '16 at 08:04

3 Answers3

3

This is because NIB/xib files can also contain more than one view representation. So within the array you can access all of the contained views.

regetskcob
  • 1,172
  • 1
  • 13
  • 35
3

The reason that xib return an array is that ".XIB" can contain multiple views. The advantage is that you don't have to create one xib file for one view. You simply just put multiple views into one XIB and access it from an array

Example
.Xib --> array[view1, view2, view3, view4]

You can see from image enter image description here

chinnawatp
  • 1,430
  • 16
  • 15
2

Thanks to the discussion here:

This is a legacy design left over from the early days of ProjectBuilder/PBX/Interface Builder. I would strongly not recommend this approach and just use separate xib files. I do not see any benefit.

However if decided to still do it, you can have multiple views as such.

enter image description here

And access View with .first or [0], view-2 with [1] and so on. Having that said in the comments JAL has said it's not guaranteed to return the indexes correctly and it's better to switch, filter or use tags for the views.

Community
  • 1
  • 1
mfaani
  • 33,269
  • 19
  • 164
  • 293