How to set view outlet for a UITableViewController? - xcode

I created an empty xib, I dragged a UITableViewController, set Custom class to my class that extends from UITableViewController at both the UITableViewController and File's Owner.
When I run, it says:
'A view can only be associated with at most one view controller at a time! View ; layer = ; contentOffset: {0, 0}> is associated with . Clear this association before associating this view with .'
At connections inspector I see that the UITableViewController element has:
tableView - Table View
view - Table View (greyed)
dataSource - Table View
delegate - Table View
First Responder has nothing.
File's Owner has:
tableView is not attached to anything
view - Table View
If I remove the view connection at File's Owner, it claims that the view outlet is not set.

It is difficult to imagine what happens based on the information provided. However:
The error message says that you have a UIView, and you try at runtime to connect this UIView to two UIViewControllers, which is not possible.
If you just subclass a UITableViewController, without adding properties or implementing methods, then setup a UITableViewController in an empty storyboard, and set the class of this UITableViewController to your subclass, you have only a single UITableViewController, and you will not get this error.
So somewhere in your project you must instantiate a second UITableViewController programmatically, and set its UIView property (which is a UITableView) to the UIView of the other UITableViewController.
You should ensure that you have only a single UITableViewController, namely your custom subclass.

I guess the correct answer is DON'T drag UITableViewController to the interface builder. Have 2 IBOutlets connected, one for the view, one for the table. DO drag a UITableView instead.
Hope this helps other people.

Related

NSOutlineView (or NSTableView) with custom NSTableCellView doesn't create connections to outlets

A .nib file contains NSOutlineView (1-column, view-based). Column has identifier "UserCell"; it also has one cell which has two NSTextField's and bunch of buttons; this cell also has identifier "UserCell". Class for this cell is set in IB to be a custom class derived from NSTableCellView class. I option-dragged both NSTextField's to the custom code (see picture attached) to create IBOutlets.
In delegate's method -outlineView:viewForTableColumn:item: I load cell using -makeViewWithIdentifier:owner: and it loads fine - I get my custom cell view object. However, both text fields are nil's.
How one should fix it? The same story with NSTableView.

How to connect outlet in a table cell view?

I am using a view-based table, and I want to create an outlet for an element in a cell view. I cannot get the outlet to connect though... it's always nil.
Specifically, I have a NSProgressIndicator in a table cell and want to manipulate it in code.
Here's what I have so far:
I have created a subclass of NSTableView, with the corresponding outlet property:
#interface MyTableCellView : NSTableCellView
#property IBOutlet NSProgressIndicator *myProgressIndicator;
#end
#implementation MyTableCellView
-(void)awakeFromNib
{
// _myProgressIndicator is nil!
}
#end
And I have set the custom class in the nib. The existing NSTableCellView is replaced with MyTableCellView via the dropdown.
At this point, some observations:
If I Ctrl+Click and drag the progress indicator to connect this outlet, it is not shown.
Likewise, if I try to Ctrl+Click and drag the progress indicator using the assistant editor, I can only connect to the property via binding. It doesn't recognize this as a valid outlet.
However this outlet IS shown on the sidebar, with a warning that it doesn't exist:
I know MyTableCellView is being used. Breakpoint on awakeFromNib confirms this, and confirms that _myProgressIndicator is nil.
This is a sandbox project, with barely more than what I've described.
SO, how do I access this progress indicator from code?
I don't believe you should do it that way; instead:
Modify the model object used by the table view's data source to populate the table view.
Call the table view reloadData (or better reloadDataForRowIndexes:columnIndexes:).
Therefore you should only need an outlet to the table view to do this and any modification of the table view's cell objects should be done within the table view delegate and/or data source methods.
As described in the question, the usual method to connect outlets in Interface Builder does not work (maybe it is fixed in a recent version of Xcode, I am still using a version earlier than 6). Anyhow, it does work via the Connections Inspector.
Create an IBOutlet in your subclass of NSTableCellView.
In your XIB, from Identity Inspector, change the NSTableCellView to your subclass.
From Connections Inspector, drag from your outlet to the View object.

Autolayout with TableViewController

I have my DetailViewController that I originally based on a UIViewController. The view uses Table View with static cells, but xcode then started to complain that I should use UITableViewController as a base class for the view. So I changed my view to be subclass of UITableViewController and wired everything again. There is a Date Picker in the bottom and when I start to use Autolayout to set constrains, xcode does not allow me to manage these under TVC. My setup works fine under ViewController.
Are there any limitations with Autolayout under TableViewController? I can also see that Table View always fills up whole view under TVC and does not allow uder subviews to exist on the same level.
Many thanks!
Only the UITableViewController can use static cells defined in the storyboard. However, it can only really show a table view, not other views as well - it's view property is the table view, which you can't just add new subviews to.
The solution is to use a table view controller to hold and manage the table, and to make this a child view controller of your main controller. This is trivial to achieve in storyboards using a container view and embed . The table view is then a subview of your main view controller's view, and you can add additional views such as the picker just as you were before.

Subviews of a NSCollectionViewItem's view are always nil

I have a very basic setup with a NSCollectionView. I have a subclassed NSCollectionViewItem which is used as the itemPrototype for the collection view. The collection view item has a view.
Using Interface Builder, I added an NSImageView to the collection view item's view and connected an IBOutlet to the instance of my collection view item subclass. The property for the IBOutlet is uses retain for its memory management.
My goal is to hide or show this image view whenever the selection state for the collection view item changes. But within the setSelected: method my property for the image view is nil, even though I am 100% sure its outlet is connected, its property is set to retain and i have not unset or released the image view.
I had a similar issue with collection view items where an activity indicator added to the view was always nil too. Why does this happen and what do I need to do to fix it?
As far as I now, IBOutlets pointing to non-top level objects in nib/xib file should be defined (weak).
Are you sure you have changed the class of NSCollectionView item correctly?
When breaking in setSelected:, and doing a po self in the debugger console, are you seeing the name of your class?

Can I use NSViewController to provide a reusable NSTableView with defined columns and data?

I'm fairly new to Xcode and Cocoa development, so please excuse me if I use the wrong terminology.
I have a window which has an NSTableView bound to a NSArrayController. The NSTableView has some predefined columns and the NSArrayController is populated with data when my window is loaded.
This all works fine, but I now need to re-use the functionality in a number of other windows.
After much reading I think and NSViewController is what I need, to provide an object that I can re-use in different windows in multiple .xib.
So what I have done is created a new NSViewController sub class in Xcode which also created a new xib for that controller.
The xib contains a custom view, where I have placed my NSTableView.
#interface KeyViewController : NSViewController
#property (weak) IBOutlet NSTableView *keyTable;
#end
The files owner class is set to the KeyViewController, and the view outlet is set to the files owner.
I then placed a ViewController object into the original window and hooked up the view to a new custom view in the window
I then set the nib name for the ViewController in its properties.
The new view never gets displayed and the view controller initWithNibName never gets called.
Am I missing something vital or missing the plot completely. Should you be able to do this just with interface builder or do I need to alloc and init the view in code?
If I do have to do it in code, whats the purpose of the ViewController object in IB and the Nib Name property for it?
Thanks..

Resources