Creating an easily reusable NSViewControlle

  • 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
previous month july 2012 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Go to today