Creating an easily reusable NSViewControlle
Creating an easily reusable NSViewControlle
- Subject: Creating an easily reusable NSViewControlle
- From: Duncan Hume <email@hidden>
- Date: Tue, 10 Jul 2012 15:51:22 +0100
Apologies if this gets posted twice, I signed up last week and tried to post and so far it has not appeared on the list (moderation backlog?). I'm trying again from a different email address!!
I have been trying to find the best way to create a re-usable component that provides everything needed to configure a view and populate it with data.
I several applications and windows where I want to place a table, without having to write lots of code in the windows.
This is where I have got to and I’m hoping someone can tell me if this is the correct approach. It seems to work, but is there a better way?
Create a new subclass of NSViewController in Xcode along with an xib
In the xib remove the custom view that is automatically generated and replace it with an NSTableView
Bind the files owner to the NSViewController subclass
Bind the NSViewController view outlet to the Scroll View for the NSTableView
Create an IBOutlet for an NSView called containerView
Implement awakeFromNib to apply the view to the window (see code below)
In the xib for the window I want to add the view to
Place a Custom NSView in the window
Drag a View Controller object to the side bar in the xib
Set the class for the View Controller to the subclassed NSViewContoller
Bind the containerView outlet in the ViewController to the Custom View
The code:
@interface DLPKeyViewController : NSViewController
@property (weak) IBOutlet NSView *containingView;
@end
#import "DLPKeyViewController.h"
@interface DLPKeyViewController ()
@end
@implementation DLPKeyViewController
@synthesize containingView;
- (void)awakeFromNib {
// Awake from nib is called twice! Only do the work once the containerView is available
if (self.containerView)
{
NSRect frame = [self.containerView frame];
frame.origin.x = 0;
frame.origin.y = 0;
// Match our view size and origin to the custom view in the widow
[self.view setFrame:frame];
[containerView addSubview:[self view]];
// I want my view to auto resize - for some reason I have to do this even tho
// it is enabled in the xib
[self.view setAutoresizingMask:( NSViewHeightSizable | NSViewWidthSizable )];
}
}
@end
The result is no code required in the window to load or alloc the view. Seems to work.
I plan to use the view controller subclass to provide the data source for the table. Then reusing it is a simple matter of importing the subclass files and xib and hooking up the objects in the window.
I hope I have explained it correctly, I'm fairly new to Cocoa and Mac dev which probably shows!
Thanks
Duncan
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden