If I am loading a view from another NIB, how do I set the File's Owner view property? IB is not letting me hook it up to my View Controller which is loading the external NIB.
My NIB looks like this:
File's Owner - Identity is set to LBRootViewController
First Responder
LBTableViewController - Identity is set to LBTableViewController, NIB Name is LBTableViewController
You can't set the File Owner to a view controller defined in another nib because you can't set outlets across nibs. Each view controller has to be the File Owner of its own nib.
No nib should have two controllers that are active at one time. The setup where you have a root controller in a nav does not actually have two controllers but instead causes the root controller to load the second controller nib at runtime.
It looks like you should have two nibs here: LBRootViewController.xib and LBTableViewController.xib. The File Owner of each nib is an instance of the classes the nibs are named for. You can create an outlet in the LBRootViewController class that points to an instance of LBTableViewController. When LBRootViewController.xib loads it does not trigger the loading of LBTableViewController.xib until the attribute serving as the outlet is accessed.
Select the File's Owner proxy (in the NIB window) and switch to the identity tab in the inspector palette (the far-right tab with the circle-i icon). Set the Class field to NSViewController (or whatever your view controller subclass is, if any). You should now be able to drag your outlet!
Related
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).
According to Apple's documentation here, we should be able to add a Page View Controller into the storyboard and then optionally set the data source by connecting the outlets.
Creating a Page View Controller Interface Using a Storyboard
The Page-Based Application Xcode template creates a new project with a page view controller as the initial scene.
To add a page view controller to an existing storyboard, do the following:
Drag a page view controller out of the library. Add a page view controller scene to your storyboard.
In the Attributes inspector, set up the appropriate options.
Optionally, set a delegate, a data source, or both by connecting the corresponding outlets.
Display it as the first view controller by selecting the option Is Initial View Controller in the Attributes inspector (or present the view controller in your user interface in another way.)
I then defined a UIPageViewController subclass like so
#interface DetailsPageViewController : UIPageViewController <UIPageViewControllerDataSource>
but then when I tried to connect the data source outlet, it does not highlight the controller or allow to connect it. I have also tried implementing UIPageViewControllerDataSource on other controllers but I have the same problem of not being able to connect the outlet.
Can anyone help?
I failed to find a way to do it in IB. Have to use the following instead:
self.delegate=self;
self.dataSource=self;
Note that the Apple documentation states that UIPageViewController is not normally subclassed. Your UIPageViewControllerDataSource does not need to be a subclass of a View Controller. You can make it a subclass of NSObject.
Normally only things that appear on the storyboard, namely UI elements, are listed in the document outline that appears to the left of the storyboard (provided it has not been hidden). If your delegate/datasource is not already there, you can put it there, by dragging an 'Object' (yellow cube) into the document outline, in the appropriate scene.
Then click on the Object that you just added, and use the Identity Inspector pane to alter its concrete class to your data source class. It's then available to be used as the target of a connection in the normal way by dragging a line from the Connections inspector onto it.
I am creating a Tab Bar Controller xib file that will house the tab bar controller. So I am highlighting my project folder for this and then creating a new file and then selecting the "Empty Template" from the i0S section on the left. But once it is created I am not seeing the User Interface that should populate when you create the NIB file. Can anyone show me what I am doing wrong possibly? When creating other XIB(NIB) files I haven't encountered this but then I again I have never used an "Empty" template for it before either.
Unlike other view controllers (e.g. UITableViewController) you should
not subclass the UITabViewController. Therefore, unlike other view
controllers, you don't subclass and then make your subclass the owner
of the nib, pointing at the view in the nib, with a customised view.
See this full ANSWER for more info of what to do it instead.
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.
I have a nib file which has a bunch of views and custom objects in it. One of those objects is a custom controller object. In it's awakeFromNib method I want to access the window that is holding all of this stuff. I'm not sure how to get the window at this point. However, this custom object has an outlet to a view. I know that you can get the window from calling [nameOfView window] but for some reason, at this point, nil is being returned for the window, even though the view is non nil. At what point will the window be non nil?
As a side note, if I pass the window as a parameter to this custom object that is loaded from a nib file, do I have to worry about releasing/retaining it? How will memory management work with this NSWindow in my object that was loaded from a nib?
If I understood you correctly, you won’t be able to do this during the nib loading process:
Some controller (let’s call it the master controller) triggers the load of that nib file;
awakeFromNib is sent to the objects inside that nib file, including your custom controller object;
The master controller gets a reference to nameOfView (potentially via the custom controller) and adds it to the view hierarchy of a window.
Loading a nib file containing views doesn’t automatically add those views to the view hierarchy of a window, which is why [nameOfView window] returns nil your custom controller’s awakeFromNib implementation. Since in step 1 there’s a master controller who’s responsible for triggering the load of that nib file, I assume this master controller is the one responsible for adding views to a window. Have the master controller send a message to the custom controller when this happens so that your custom controller knows when nameOfView has been added to a window.