I have an NSTabView that has different styles of NSTabViewItems. Is there a way that I can separate out the tab controller into a different class than my outerview controller? Just so that I can keep my code tidy. I'm also looking to keep the tab contents in a separate nib.
Note: This is for the NSTabViewItem NOT for UITabs (not the iPhone).
Sure, but it takes a bit of setup in code. Create the separate NIBs, one for each view. Create a custom subclass of NSViewController to load and own each NIB and control the view within. Set the class of each view NIB's File's Owner to be the corresponding class. Connect outlets as desired.
The tab view would be basically empty when you configure it in its NIB. You'd add the tabs, but nothing in the views.
In the controller for the NIB with the tab view, in -awakeFromNib, instantiate the view controllers for each of the views that should go into the tab view. Set the views of these new view controllers as the views of the NSTabViewItems of the tab view.
Related
I've been transitioning my programmatically-created user interface over to interface builder, but I'm still fairly new to IB. I have a custom view inside of my main view (in the IB .xib file), which I want to link to its own view class, and, more importantly, its own view controller.
I know that traditionally, one references the custom view class from the IB property, and then uses -awakeFromNib in the custom view class to set up the view programatically.
However, I can't figure out how to reference the custom view controller class. How to I tell my custom view what it's controller should be? And is there a way to instantiate a view controller from IB, or should I be doing it from -awakeFromNib?
Advice would be appreciated :)
Obj-C please!
I have a table view inside a normal view controller and I was wondering how I should go out populating it with data?
After researching it, everywhere I have seen it done uses a table view controller: I am just using a view controller with a table view inside it.
How should I populate this with data? I have created an IBOutlet in my ViewController.swift, but I'm not sure what to do to add data.
The reason I am not using a table view controller is because I couldn't get the navigation bar to show up and I heard that the navigation bar scrolled.
Is it possible to use a table view inside a normal view controller? Should I be using table view controller? If it is possible to use a table view inside a normal view controller, how do I populate the table view?
The coding language I am using is swift.
A UITableViewController is basically a normal UIViewController which has its main view replaced with a UITableView and uses the UITableviewDelegate and UITableViewDataSource protocols.
UITableViewController is subclass of UIViewController.
So just add the protocols and use it exactly the way you use it in UITableViewController.
I'm using Storyboards for the first time creating a master/detail view for a simple object.
Both view controller items of the split view have their own view & NSObjectControllers. The controller for the table view is a NSArrayController (which shows all instances of the Entity). The controller for the detail view is an NSObjectController, which shows just the selected instance.
When I click an object in the table, I'd like the detail to show up in the detail view. So I believe my goal here is to bind the 'contents' of the detail object controller to the 'selection' of the master array controller. However with storyboards I cannot cross view controller boundaries (can't create the IB 'link'. aka: can't bind cross VC).
Whats the best practice for doing this?
So far all I can think to do is manually set all this up in one of the controllers (either the root controller, window controller or perhaps the master table view controller) - but that seems counterintuitive. That seems to defeat the purpose of being able to lay out this stuff within storyboards in the first place, and my controller of choice would then have to know about the other controller specifically.
I must be missing something!
I'm developing a Mac Application. The application has a common source view on the left and a detail view on the right which is the main part of the whole window.
It's like a Master-Detail relationship, but each element in the source view require another detail view. In fact, I have designed a specific NSViewController for each element in the source view.
If I'm switching between these NSViewControllers, that means If I select another element in the source view, I remove the current view and add the view of the newly selected NSViewController. Everytime I change the NSViewController, its state will be lost. When the user comes back to that NSViewController, he has to start over.
My question now is: How can I save the state of the NSViewController, so that I can switch between these without losing its states and can continue where I have left?
Two considerations about your problem:
Keep model data in model classes. This means that you can always recreate a view controller and set its represented object provided the model classes have kept the changes made via the view controller. When you need to instantiate a view controller, set its represented object to (a representation of) a model class.
When removing a view from its superview, you do not necessarily need to release its corresponding view controller. Instead, you can keep strong references to all view controllers in your window controller/application delegate, so no state is actually lost.
Use NSArchiver. Implement archiving/unarchiving in your dealloc/init methods and store each view controller's state in a file named after the class (if you have one item per view controller policy). Otherwise think of some simple naming convention and use it.
(Abstract: bindings work in code, but not in IB)
I have a window managed by a NSWindowController. To the left of the window is a source view. To the right is a table view showing the elements of the currently selected source.
I have set up a NSTreeController within my window XIB. I want its contents to be used for the source view. It's selection will drive the table view.
I am trying to split this up using NSViewControllers. One view controller will load a NIB containing the source view. Another view controller will load the table view.
Seeing that I need access to the NSTreeController within the source view controller, I have set it to be the view controller's representedObject. (Actually for this setup to be done by the time awakeFromNib is called on the view controller, I have turned representedObject into an IBOutlet).
All works fine when I wire my source view up in code:
[outlineView bind:#"content"
toObject:sources
withKeyPath:#"arrangedObjects"
options:nil];
[outlineView bind:#"selectionIndexPaths"
toObject:sources
withKeyPath:#"selectionIndexPaths"
options:nil];
[[outlineView tableColumnWithIdentifier:#"Title"] bind:#"value"
toObject:sources
withKeyPath:#"arrangedObjects.title"
options:nil];
I am however unable to reproduce this using Interface Builder. Thing is, here the "controller key" textfield is grayed out. Thus I bind column's "value" to the file owner using a model keyPath of "representedObject.arrangedObjects.title". This does not show the desired behavior. Actually an exception is thrown: -[NSProxy doesNotRecognizeSelector:_mutatingNodes] called!
How can I use representedObject in IB?
Can I create a controller in IB which acts as proxy to representedObject?
Could I set-up a tree controller in the source view XIB which during NIB loading gets swapped out for the representedObject?
I moved away from using representedObject. It appears that is meant only for model objects.
I now pass in my tree controller using a custom outlet. I continued setting up and tearing down the bindings in code.
I’ve similar issues when I try to pass a reference to an object controller (NSTreeController in my case). I don’t think this is how Apple wants you to use their KVO-compatible controllers. The exceptions look like they’re XIB-unarchiving & timing-related.
The trick is not to pass the controllers, but to pass the underlying data and keep the selection in sync.
This way you can set up your bindings in a storyboard and won’t get any exceptions. You’ll have to set up a new instance of an object controller for every child view controller (copy & paste in Storyboard once you configured the first one works). For a detailed example take a look at another answer that gets much more into detail.