0

For example:

NSBundle.mainBundle().loadNibNamed("Games-New", owner: nil, options: nil).first

How can there possibly be multiple views?! I only drag one view from the storyboard and then put a label/image inside of it and am done. Or does this mean I can drag multiple container-like views and the first view I drag out would be the first or something? (that would be a pretty bad idea)

EDIT

Why would it ever be a good idea to group things? I thought the whole idea is to decouple different elements/views. Isn't doing such exactly the opposite?

mfaani
  • 33,269
  • 19
  • 164
  • 293
  • 1
    You can add whatever objects you want in a nib, not just views. If you add a gesture recognizer to your view then it would be in the array too. – dan Jan 09 '17 at 17:54
  • @dan It's seems a bad design. I mean the whole point is to separate things into smaller pieces. Why would you add another view, or a gesture? Also how are you going to differentiate between a gesture and a view? Your answer makes the idea to seem worse. :D – mfaani Jan 09 '17 at 18:06
  • You don't **need** to use first. You access the index of the view you need to unarchive from the nib. In most cases, there is only one view, so you use `fist`. In other cases, [there can be multiple views](http://i.stack.imgur.com/5S6jD.png). – JAL Jan 09 '17 at 18:08
  • @Honey Who says the whole point is to separate things into smaller pieces? The point of nibs is to set up views without needing to write code. If your view needs to have some other object accompanying it then allowing you to put it in the nib makes perfect sense. – dan Jan 09 '17 at 18:08
  • @dan *If your view needs to have some other object accompanying it* you mean I would need to access two indexes of a xib file? Like let me get index1 which a tableviewCell & index5 which is Custom UIView, but since we theses two views were very much related we decided to group them together yet at some times the index5 isn't needed which is why we don't put them exactly into 1 view? – mfaani Jan 09 '17 at 18:13
  • @JAL I just added another view into my xib and looked into the inspector to find which one has which index, but i wasn't able to find out, or is it solely based on the index of what you just showed in the image? – mfaani Jan 09 '17 at 18:20
  • I believe it should be from top to bottom, but it might be a case where the order is not guaranteed, like an `IBOutletCollection`. – JAL Jan 09 '17 at 18:21
  • @JAL what do you mean not guaranteed? I'm talking about different views dragged into a Xib file. view1,view2,view3, tableview1,collection2, etc. So how is the developer suppose to figure out their index? I mean if it's not visually guaranteed or isn't available in the inspector and say I have 10 objects, am I suppose to just do loop through an array and use if statements? – mfaani Jan 09 '17 at 18:26
  • I think this is a legacy approach left over from the early days of iOS development. I wouldn't recommend it, but you could use tags on the views. Only Apple can answer the question why they implemented this functionality. – JAL Jan 09 '17 at 18:27
  • so you mean other than that it's not guaranteed (which is rare) and you must loop through and catch the view you intend with an if statement. Am I getting this right? – mfaani Jan 09 '17 at 18:28
  • 1
    If you want to use that approach, yes, you can't get around iterating through the array. There are other ways to check the views besides an `if` statement though. You could use `filter`, `switch`, remap the array to a dictionary, etc. – JAL Jan 09 '17 at 18:29
  • @JAL am I missing something? is there any benefit to this poor design? :D – mfaani Jan 09 '17 at 18:31
  • 1
    Like I said, 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. – JAL Jan 09 '17 at 18:32

1 Answers1

1

Even though you dragged only one view there - it's still prepared to support multiple views inside on XIB files.

If you create more views in the same file, you will be able to pick the correct one by using proper index value.

One idea would be to keep all UITableViewCells in 1 XIB file - and iterate over the classes to get the one you are looking for, by comparing a class to the one you are looking for. It's still not recommended in my opinion - since it's easier to keep views in seperated xib files - especially if there are multiple people working on the same project (to avoid conflicts).

Grzegorz Krukowski
  • 18,081
  • 5
  • 50
  • 71