What's meant by File's Owner exactly? - cocoa-touch

What is meant by "File's Owner"? The XIB interface says it's UIApplication, but why is it named so? Which file does it own? I understand MVC to some extent, but I never heard of "File's Owner". What does it have to do with the controller of the application?

File's Owner is a proxy for the object that's specified as the owner when the .xib is loaded. Usually, it's the object that's actually loading the .xib. In any case, it's important to realize that File's Owner represents some object that's external to the objects in the .xib file, and as such it's basically the way that objects inside the .xib are connected to something outside the .xib and vice versa.
This all has very little to do with MVC and a lot to do with how Interface Builder works. Typically, you add IBOutlet properties and IBAction methods to the object that will load a .xib (such as an application delegate or a view controller). Then, using IB, you connect objects inside the .xib to the File's Owner proxy, and you set File's Owner as the target of your controls (choosing the appropriate action for the control).

Related

What object should implement NSCollectionViewDelegate?

I have a subclassed NSCollectionView that is created in Interface Builder and is bound to an array.
I need to implement some of the delegate classes from NSCollectionViewDelegate but I'm kind of stumped as to what class to designate as the delegate.
Do I just create a random NSObject and designate it as the delegate? Do I create an NSViewController and designate that as the delegate—but if so, since the Collection View isn't being created programmatically, how do I tie the NSViewController to the Collection View?
Any pointer to a good document that would explain stuff like this in Mac app architectural decisions would be a great bonus.
It is common, but not mandatory, to make a view controller also be the delegate of the underlying view object. However, you can also make a new class as you suggest and use that. If that works for your overall architecture, do that.
What kind of object is the File's Owner of the NIB? Most often, a NIB should be loaded and owned by either an NSWindowController or NSViewController (or subclass of one of those). That controller would typically be responsible for everything in the NIB, so it would be the delegate.
If it makes sense, you might put the collection view into a NIB all its own. You'd create a subclass of NSViewController to load and own the NIB and set the class of the File's Owner placeholder in the NIB to that class. You'd connect the view outlet of File's Owner to the collection view and the delegate outlet of the collection view to File's Owner.

Xcode 6.1 cannot drag connection to a property from XIB

I've just started with Xcode and I'm having a certain difficulty. I've placed a label on the canvas, declared property in viewcontroller.m, and now I'm trying to draw a connection into it, but it doesn't seem to exist. What am I doing wrong ?
I've added a screenshot - the property is not on the list for some reason, as you can see.
Using Xcode 6.1!
http://i.imgur.com/xYfoCnh.png
Select your view in your xib file and then click on File's Owner here:
With File's Owner selected, then open the identity inspector and make sure the class of File's Owner is ViewController like so:
Once you set the class of File's Owner to ViewController, you should see the UILabel property appear in the Connections Inspector for File's Owner.
Also, just for reference, the File's Owner for a view in a Xib file typically refers to the view controller (the view controller owns its view).

NSViewController and bindings

In my application I have a single nib file. The File's Owner is a NSViewController and insider there is just a vertical slider that I want to bind to a property in one of my classes. (I don't have any other nib files since it is a status bar application, so I don't have a window). The nib is loaded runtime to create a custom view for a NSMenuItem.
The problem is that I want to use an object controller to do the binding but I'm not sure what is the content of the object controller. How can I access from the nib to an arbitrary class in my project?
In the examples I have seen, usually the object controller uses the File's Owner to access the class (and the property for the binding) setting the Content Object binding to the File's Owner. But in my case from the File's Owner I do not have access to the class.
Any pointer?
Ok this works for me. It is enough subclassing NSViewController and sets the new subclass as the File's Owner. Now it is possible to use a Controller Object to bind through the File's Owner.
If you use XCode 4 you can Control-drag from the slider to the .h file (AppController.h?), and you will get the option to generate an outlet or an action. If you want to create an action, caused by sliding the slider, you should select action. The generated method will be called whenever the slider is changed. If you want the slider value to react to an event (or a changed instance variable in your program), set it to 'outlet'.
Hope that helps, let me know if you were really looking for something else :-)

Cocoa's NIB/XIB Confusion

Just a newbie with iPhone development.. I just have a small problem but huge confusion with regards to NIB files. In Cocoa, how can we change the File's owner and First responder of a nib? And BTW, when can we actually say that we need more than one NIB file for a project in the first place?
Change the class of File's owner in Interface builder (select File's owner and open Identity inspector). You don't need to change anything in First responder.
As to your second question - you can build an entire project programmatically without nibs. I build mine with nibs for every view, controller, custom control etc.
Open the nib, click on "File's Owner"
Select the third inspection tool, "Identity Inspector" (?)
Type in the class this nib needs to be associated with.

Can I define an outlet in a .nib without specifying a controller?

I have a nib that has a button that I would like the relevant controller to have access to. But right now the owner is a generic NSViewController, and inside AppDelegate I assign the nib to the controller like:
[browseViewController initWithNibName:#"BrowseView" bundle:[NSBundle mainBundle]];
But is it possible within the nib file to assign a button's referencing outlet generically? In other words, to give the outlet a name and point it to File's Owner, and then if/when the nib is connected to the controller if that controller has an outlet with the same name it just works?
The File Owner has to have the certain outlet and/or the action when you connect it in IB. Even if you could connect it to an outlet that does not exist, an exception will be raised when the nib is being loaded. I you want to check it make an outlet, connect it to an object, save the nib, delete the outlet from the object and run the application. NSUnknownKeyException will be raised.
The easiest way would be to use a common superclass of your controller. If the controllers cannot be derived from the common ancestor, the only workaround I see is to make a protocol where you define your outlet properties and actions. Make an object that conforms to the protocol
MyObject: NSObject < MyNibProtocol >
now use this object in the nib to connect the outlets and actions.
However, the properties and methods cannot be optional, you have to implement them.