View-based NSTableView From Xib - objective-c

I have a NSTableView that is created programmatically. I have several options for customizing the cells in each column based on the column type and datasource (ie, it's very easy to have buttons or checkboxes based on the column type and what is in the datasource).
Now I need to be able to fully customize the cell, so I'm attempting to load an NSView from a xib and return it from the tables delegate's viewForTableColumn method. I haven't used IB much outside of iOS and I'm not very well versed as to how the various outlets and class types should be set, especially when the majority of the UI is created outside of IB. I've read many posts here and on other sites but the majority of examples either create all of the UI in IB or none of it.
Currently I have TestCell.xib which was created by selecting View from the New File dialog. I've also created an objective-c class called TestCell. In IB I've set the view's class to TestCell, and I've dragged outlets for a label control and a button to the TestCell class.
In the table's delegate class I have the following:
- (NSView*)tableView:(NSTableView*)tableView viewForTableColumn:(NSTableColumn*)tableColumn row:(NSInteger)row {
NSView* view = [tableView makeViewWithIdentifier:customRowXibName owner:self];
if( view == nil ) {
NSArray* nibObjects = nil;
if( [[NSBundle mainBundle] loadNibNamed:customRowXibName owner:self topLevelObjects:&nibObjects] ) {
view = [nibObjects lastObject];
}
}
return view;
}
However, the table view doesn't show anything. I'm also getting the following errors for both controls in the view when loading the xib:
Failed to connect (button) outlet from (TableListViewDelegate) to (NSButton): missing setter or instance variable
I'm assuming that's because I'm setting owner to self when loading the xib.
My questions are:
In IB, what should the File's Owner placeholder be set to? Currently it's set to TestCell but I don't believe that is correct.
Is it ok to use "TestCell" as the identifier? Does this identifier need to be set in IB? Or do I need to call registerNib:forIdentifier on the table view?
When calling loadNibNamed, what should owner be set to?

I was able to get this to work by doing the following:
In IB, set the File's Owner to be the class that is loading the Xib (in this case, the NSTableViewDelegate).
In the delegate, create an outlet for your custom cell and hook it up in IB (I used the Connection Inspector with the File's Owner selected).
In tableView:viewForTableColumn:row call:[tableView makeViewWithIdentifier:#"Xib Name" owner:self]
If that returns nil, then call:[[NSBundle mainBundle] loadNibNamed:#"Xib Name" owner:self topLevelObjects:&nibObjects] with nibObjects being a nil NSArray*.
If loadNibNamed returns YES, then the outlet you created in the delegate should now point to the newly loaded view. Make sure to set the views identifier to #"Xib Name" so you can make use of cached views.

Related

Container View Controllers pre iOS 5

iOS 5 adds a nice feature allowing you to nest UIViewControllers. Using this pattern it was easy for me to create a custom alert view -- I created a semi-transparent view to darken the screen and a custom view with some widgets in it that I could interact with. I added the VC as a child of the VC in which I wanted it to display, then added its views as subviews and did a little animation to bring it on the screen.
Unfortunately, I need to support iOS 4.3. Can something like this be done, or do I have to manage my "alert" directly from the VC in which I want to display it?
MORE INFO
So if I create a custom view in a nib whose file owner is "TapView" and who has a child view that is a UIButton. I tie the UIButton action to a IBAction in TapView.
Now in my MainControllerView I simple add the TapView:
TapView *tapView = [[TapView alloc] init];
[[self view] addSubview:tapView];
I see my TapView, but I can't interact with the UIButton on it and can interact with a UIButton on the MainControllerView hidden behind it. For some reason I am not figuring out what I'm missing...
Not sure if this helps, but, in situations where I've needed more control over potential several controllers, I've implemented a pattern where I have a "master" controller object (doesn't need to be descendent from UIViewController), which implements a delegate protocol (declared separately in it's own file), and then have whatever other controllers I need to hook into declare an object of that type as a delegate, and the master can do whatever it needs to do in response to messages from the controllers with the delegate, at whatever point you need; in your case, that being displaying the alert and acting as it's delegate to handle the button selection. I find this approach to be very effective, simpler and usually cleaner. YMMV ;-)
Regd. your second query, where you are trying to create a custom view using nib. Don't change the FileOwner type, instead set "TapView" for the class property of the top level view object.
Once you have done this, you might experience difficulty when making connections. For that just manually choose the TapView file for making connections.
Also, to load the view you need to load its nib file. For which you can create a class level helper method in TapView class like below
+(TapView *) getInstance
{
NSArray *bundle = [[NSBundle mainBundle] loadNibNamed:#"TapView" owner:self options:nil];
TapView *view;
for (id object in bundle) {
if ([object isKindOfClass:[TapView class]]) {
view = (TapView *) object;
break;
}
}
return view;
}
Now, you get a refrence to you view object like this
TapView *tapView = [TapView getInstance];

Holding NSView instances in an array

Is it possible to store an NSView object in a mutable array? As I understand it, the view will be an object so the array should be able to hold it. Specifically, I want to hold several instances of a nib file, which I think would be loaded with an NSNib init, and then addObject to the array.
The idea is to display an NSView in each of the rows of a column in a TableView. I think it can be done because iTunes does something similar (with what I think is an NSImage) in displaying album artwork in a list view.
Still, any knowledge on the subject (or link to an example or tutorial) would be very appreciated.
TableViews usually don't hold an NSView for each item. They hold a number of NSViewTableCells (which are, system-wise, far more lightweight than NSViews), and they re-use these cells. They usually don't have many more cells than necessary to display the visible part of the TableView, AFAIK, and when the view is scrolled, cells that have become "invisible" are re-used.
So the best way to do this is to subclass the cell and to make the TableView display the contents using these. Using NSViews for every entry in a list of, say, my MP3 albums would be extremely expensive.
In Xcode goto File->New File and choose Objective-C class then in the drop down, choose to make it a subclass of UITableViewCell. Name it MyCell (for example)
Next in interface builder, create a new XIB and change its owner to your newly created class. You may also want to delete the default view and add a UITableViewCell. Set the owner's view outlet to this new tableview cell. Then add whatever you want to the UITableViewCell.
Then create a new UIViewController (which it may be helpful to create a new UITableViewController first and then change its type to UIViewController just so you get all the UITableViewDelegate methods added for you) and choose to add a XIB file for the UIViewController. Open the header file for the newly created UIViewController and add UITableViewDelegate protocol so the header may look like this:
#interface MyViewController: UIViewController <UITableViewDelegate, UITableViewDataSource>
On the view for the view controller (in interface builder) add a UITableView and then set its datasource and delegates to the owner. Make sure to import the MyCell.h header file. Then implement the tableViewMethods in particular for your UITableViewCell you would do something like this:
- (UITableViewCell *)tableView:(UITableView *) tableView
cellForRowAtIndexPath:(NSIndexPath *) indexPath
{
static NSString *MyCellIdentifier = #"MyCellIdentifier";
MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:MyCellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"MyCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
//Here you would set properties of the cells items like labels, etc.
return cell;
}

Using the same NIB with multiple View Controllers

Basically I want to use a nib file and view controller as a template for a view that I plan to create a number of times. This nib will have a couple of labels and custom views.
The idea is that I will iterate through an array of objects and for each I will create an instance of this controller and set a property to that of the object from the array.
This all works nicely at the moment except for one thing - the labels won't update when I call setStringValue: !!!
I'm using a method within the view controller's code to make the change but it just doesn't work, I'm guessing that the IBOutlet isn't being hooked up properly, which is strange because the custom views are hooking up perfectly.
Any ideas?
Set a breakpoint on awakeFromNib and look in the debugger what the value of the label outlet is. All outlets should have been connected before awakeFromNib is being called. If it is still nil, you have your answer. Calling setStringValue: on nil does exactly "nothing". In that case you have not correctly bound the outlet or maybe you once had it bound correctly and later on changed the name, in that case there should be a yellow warning triangle in Xcode4 or interface builder indicating that something is wrong; however it will not prevent your app from building or running, the outlet will simply keep its initial value after object creation (which is nil).
When you alloc your NSViewControllers, just init with name of NIB:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
Thanks for the replies, they were helpful but not quite what I was getting at.
I ended up solving it by creating an empty NIB and filling it with just a custom NSView and a few other controls. I created an NSView subclass with IBOutlets for those controls and set the custom view's identity to my subclass in interface builder.
The trick in getting it to work each time I wanted to draw it was by making a class method in my subclass that would load the nib and return the view set up the way I wanted.
Code below:
+(id)todoViewFromNibWithFrame:(NSRect)frameRect todoList:(TodoList *)aTodoList
{
NSNib *todoViewNib = [[NSNib alloc] initWithNibNamed:#"TodoView" bundle:nil];
NSArray *objects = nil;
id todoView = nil;
[todoViewNib instantiateNibWithOwner:nil topLevelObjects:&objects];
for (id object in objects) {
if ([object isKindOfClass:[self class]]) {
todoView = object;
[todoView setTodoList:aTodoList];
break;
}
}
[todoViewNib release];
return todoView;
}
Thanks again for the replies!
Steve

iPhone subview design (UIView vs UIViewController)

I'm designing a simple Quiz application. The application needs to display different types of QuizQuestions. Each type of QuizQuestion has a distinct behavior and UI.
The user interface will be something like this:
alt text http://dl.getdropbox.com/u/907284/Picture%201.png
I would like to be able to design each type of QuizQuestion in Interface Builder.
For example, a MultipleChoiceQuizQuestion would look like this:
alt text http://dl.getdropbox.com/u/907284/Picture%202.png
Originally, I planned to make the QuizQuestion class a UIViewController. However, I read in the Apple documentation that UIViewControllers should only be used to display an entire page.
Therefore, I made my QuizController (which manages the entire screen e.g. prev/next buttons) a UIViewController and my QuizQuestion class a subclass of UIView.
However, to load this UIView (created in IB), I must[1] do the following in my constructor:
//MultipleQuizQuestion.m
+(id)createInstance {
UIViewController *useless = [[UIViewController alloc] initWithNibName:#"MultipleQuizQuestion" bundle:nil];
UIView *view = [[useless.view retain] autorelease];
[useless release];
return view; // probably has a memory leak or something
}
This type of access does not seem to be standard or object-oriented. Is this type of code normal/acceptable? Or did I make a poor choice somewhere in my design?
Thankyou,
edit (for clarity): I'd like to have a separate class to control the multipleChoiceView...like a ViewController but apparently that's only for entire windows. Maybe I should make a MultipleChoiceViewManager (not controller!) and set the File's Owner to that instead?
You're on the right track. In your QuizController xib, you can create separate views by dragging them to the xib's main window rather than to the QuizController's main view. Then you can design each view you need according to your question types. When the user taps next or previous, remove the previous view and load the view you need based on your question type using -addSubview on the view controller's main view and keep track of which subview is currently showing. Trying something like this:
[currentView removeFromSuperView];
switch(questionType)
{
case kMultipleChoice:
[[self view] addSubview:multipleChoiceView];
currentView = multipleChoiceView;
break;
case kOpenEnded:
[[self view] addSubview:openEndedView];
currentView = openEndedView;
break;
// etc.
}
Where multipleChoice view and openEndedView are UIView outlets in your QuizController connected to the views you designed in IB. You may need to mess with the position of your view within the parent view before you add it to get it to display in the right place, but you can do this with calls to -setBounds/-setFrame and/or -setCenter on the UIView.
Yeah, IB on iPhone really wants File's Owner to be a UIViewController subclass, which makes what you want to a bit tricky. What you can do is load the nib against an existing UIViewController instead of instantiating one using the nib:
#implementation QuizController
- (void) loadCustomViewFromNib:(NSString *)viewNibName {
(void)[[NSBundle mainBundle] loadNibNamed:viewNibName owner:self options:nil];
}
#end
That will cause the runtime to load the nib, but rather than creating a new view controller to connect the actions and outlets it will use what you pass in as owner. Since we pass self in the view defined in that nib will be attached to whatever IBOutlet you have it assigned to after the call.

Design an NSView subclass in Interface Builder and then instantiate it?

So I have an NSTabView that I'm dynamically resizing and populating with NSView subclasses. I would like to design the pages in IB and then instantiate them and add them to the NSTabView. I got the programmatic adding of NSView subclasses down, but I'm not sure how to design them in IB and then instantiate them.
I think I got it. Let me know if this is not a good thing to do.
I made a new xib file, set its File's Owner to be an NSViewController and set its "view" to the custom view I designed in the xib.
Then you just need:
NSViewController *viewController = [[NSViewController alloc] initWithNibName:#"MyViewXib" bundle:nil];
NSView *myView = [viewController view];
#toastie had a really good answer. Mine is similar, but requires a bit more explanation.
Let's say you've already got a controller object and you don't want to instantiate a new controller object just to get at a view, and let's say that you're going to need multiple copies of this view (for example, you've designed a custom UITableViewCell in IB and you want to instantiate it again and again from your UITableViewController). Here's how you would do that:
Add a new IBOutlet to your existing class called "specialView" (or something like that). It may also be helpful to declare it as a (nonatomic, retain) property.
Create a new view xib called "SpecialView", and build the view however you like.
Set the File's Owner of the view to be your controller object.
Set the specialView outlet of File's Owner to be the new view.
Whenever you need a new copy of the view in your code, you can simply do the following.
(gratuitous text to get formatting working properly)
NSNib * viewNib = [[NSNib alloc] initWithNibNamed:#"SpecialView" bundle:nil];
[viewNib instantiateNibWithOwner:self topLevelObjects:nil];
[viewNib release];
NSView * myInstantiatedSpecialView = [[[self specialView] retain] autorelease];
[self setSpecialView:nil];
Yes, it's a bit more code than other ways, but I prefer this method simply because the view shows up in the designated IBOutlet. I retain and autorelease the view, because I like to reset the outlet to nil once I have the view, so it can be immediately ready to load a new copy of the view. I'll also point out that the code for this is even shorter on the iPhone, which requires one line to load the view, and not 3 (as it does on the Mac). That line is simply:
[[NSBundle mainBundle] loadNibNamed:#"SpecialView" owner:self options:nil];
HTH!