5

Apologies in advance for this question, though it's been discussed at length since iOS came out. But I still don't get it, despite my best efforts. The closest image I can picture is "frozen and dried object graph", which is fine, but that doesn't help connecting the dots when it comes down to raw programming.

So, considering you can't really describe anything until you figure out what is does, let's take the following example:

1) I have a Xib file, say a UIView, with labels, and imageView

2) I have a classX, subclass from a UIView, that was attached to the Xib in IB's identity inspector. IB outlets, and IB actions are connected to this class, in the connection inspector. This class declares the IB/IA as properties.

3) I have a UIViewController, represented by a classVC. In this class I instantiate 1) using the standard [[NSBundle mainBundle] loadNibNamed:@"bla" owner:??? options:nil][0];

4) classX and classVC communicate via custom delegates, standard stuff.

Questions:

a)What is the XIB file's owner. Honestly, I have no idea.

b)what do I put in [[NSBundle mainBundle] loadNibNamed:@"bla" owner:??? options:nil][0]; for the owner parameter, and most importantly, why ?

When the object graph is unfrozen and watered, shouldn't the runtime connect the Outlets has defined in 2) so what's the point of the file's owner in this case?

OR

If the XIB file Outlets are not connected to anything, does "File's owner" mean "I'm the class containing all the required OUtlets so that connections between that class instance and the decompressed XIB can be made ? (that's why I don't get it, because how would the association be made ???)

So, that's it. Just as dumb as before.

EDIT: I disagree with the fact that other answers are equally valid. I understand what they meant, but so far I have yet to understand what the owner parameter should hold for the case I describe, and most importantly, why.

Alex
  • 1,581
  • 1
  • 11
  • 27
  • 1
    Here is a great answer to: [1]: http://stackoverflow.com/questions/3768602/what-actually-is-file-owner-and-first-responder-in-iphone-sdk-xcode – Adrian P Mar 20 '13 at 00:33
  • 1
    Apple has replaced XIBs with storyboards which no longer have a files owner – vqdave Mar 20 '13 at 01:41
  • 1
    @vectorquake Apple certainly has not replaced XIBs with storyboard. The two are co-existing and will always coexist. – Till Mar 20 '13 at 02:09
  • @CodeMonkey what I don't get is if it's so important why setting the value to nil ? if it's the class that owns the IBoutlets, why not writing MyClass* XYZ =[[NSBundle mainBundle] loadNibNamed:@"NibName" owner:XYZ options:nil]; ? – Alex Mar 20 '13 at 09:56
  • @Alex That doesn't work because the owner must be a previously instantiated object (e.g. your view controller). If `XYZ` is a view subclass, if you wanted to link `IBOutlet` and `IBAction` references to the view subclass itself, you'd (a) specify the custom class for the view, itself, in IB; and (b) you'd link the actions and outlets to that, not the file's owner. Obviously, if you have an IBAction you want linked to the "file's owner" (e.g. the controller), you can do that, too. But you'd never set the owner of a view created by a NIB to be itself. – Rob Mar 20 '13 at 16:57
  • See [About File's Owner](https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/LoadingResources/CocoaNibs/CocoaNibs.html#//apple_ref/doc/uid/10000051i-CH4-SW15). Bottom line, the "owner" is important if you're linking outlets and actions to it. But if you specify a custom class for your view in IB, you might not need any references to the owner at all. It just depends upon what you're doing. Personally, I'll tend to use `self` (e.g. of the view controller who was calling this) as the owner, but it's not uncommon for the NIB to not make any references to the owner. – Rob Mar 20 '13 at 16:58
  • @Rob thanks, that was also my impression. The only way I can understand [[NSBundle mainBundle] loadNibNamed:@"NibName" owner:??? options:nil] is by doing the following. 1) MyClass* XYZ = [MyClass alloc]init]; and 2) [[NSBundle mainBundle] loadNibNamed:@"NibName" owner:XYZ options:nil]; Where MyClass is associated to this NIB in IB. in that case XYZ has all outlets/actions are set properly. So, if you set it to self, and if self isn't an instance of the class associated to the NIB - ex: self is a controller, the NIB a UVIview-, how do you get the Outlets and IActions connected properly ? – Alex Mar 20 '13 at 19:06
  • It depends upon what `MyClass` is. If it's a generic `NSObject` for which you have, for example, some `IBAction` methods defined, then your approach sounds good. But if it's a `UIView` subclass intended to represent the view created in the NIB, then I think specifying it as the owner is not right (unless what is in your NIB is for a subview of another view). Bottom line, I don't believe the owner should be the view specified within the NIB, but some other, pre-existing object (controller, view, etc.) which will own the objects that iOS creates from the NIB. – Rob Mar 20 '13 at 19:18
  • @Alex If you want to discuss further (e.g. go through some illustrative examples), let me know, and we can set up a chat. – Rob Mar 20 '13 at 19:20
  • @Rob, sure I think a simple example will help me getting to the bottom of this stuff. How does a chat on SO work ? – Alex Mar 20 '13 at 19:32
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/26573/discussion-between-rob-and-alex) – Rob Mar 20 '13 at 19:49

0 Answers0