Last week I asked how I could create a view xib + controller and use it in multiple applications (eg. a stand alone test and an app that integrates several of them.)

Several people responded to my request last week with some tantalizing leads.
The best was Chris Hansen's pointer:
This is a really handy way to create and manage re-usable content for
Cocoa applications.  There are some great articles here:

    NSViewController, the New C in MVC (parts 1 through 3)
    
http://katidev.com/blog/2008/04/09/nsviewcontroller-the-new-c-in-mvc-pt-1-of-3/
    
http://katidev.com/blog/2008/04/17/nsviewcontroller-the-new-c-in-mvc-pt-2-of-3/
    
http://katidev.com/blog/2008/05/26/nsviewcontroller-the-new-c-in-mvc-pt-3-of-3/

However, this nice article shows how to do it with a Document based application. My app is not document based and I couldnt figure out how to get it working with Kati's hints I consulted a local cocoa guru and with a bit of self-actualizing, I got it working. I did not use the XS... stuff in the Kati article. I'm not sure what that would buy us and it had some dependencies that didnt fit with our solution.

To benefit the community.... and myself when I forget this in 6mo, I'm posting my (partial) solution here. These are a bit cryptic notes, rather than flowing prose and insights ala Katidev, but will hopefully suffice:

Making a reusable View

Application setup -

In XCode
  File->New Project
        select Cocoa Application
  select Classes group
  Ctrl-Select Classes -> Add->New File : NSApplication Controller
        name it AnApplicationController
  in the .h @interface section insert
        IBOutlet NSWindowController *windowController;
        NSViewController *viewController;
  in the AnApplicationController.m insert:

#import "TheViewController.h"

- (id)init
{
        if ( self = [super init] )
        {
// substitute yourView's nib + controller names in next line viewController = [[TheViewController alloc] initWithNibName:@"TheView" bundle:nil];
        }
        return self;
}

- (void)awakeFromNib
{
[viewController.view setFrame:[windowController.window.contentView frame]];
        [windowController.window.contentView addSubview:viewController.view];
}


Ctrl-Select Classes -> Add->New File : Objective-C NSWindowController Subclass
        name it AWindowController
  Double Click MainMenu.nib to open Interface Builder


In Interface Builder
  open Library
  drag an Object into MainMenu.nib
  open the Inspector
  change class to AnApplicationController
  drag an Object into MainMenu.nib
  select object, view in the Inspector
  change class to AnApplicationController
connect window to its controller by Control Drag from WindowController to Window, connect appController to windowController by ctrl-Drag from appCtrl to window Ctrl


------
Creating the View
In XCode
  Ctrl-Click project: Add->New Group
        call it TheView
  Ctrl-Click TheView: Add->New File ->Objective-C class
        name it TheViewController
        in the .h, change its superclass to be NSViewController
Ctrl-Click TheView: Add->New File -> Interface Builder -> Cocoa- >View Nib
  Add IBOutlets, IBActions, etc as needed to TheViewController

Open TheView.nib in InterfaceBuilder
select File Owner, in Inspector set its class to be The ViewController
  control drag from TheViewController to the View
  Add widgets to the view, connect them to TheViewController

That should do it.
Of course "TheView" can be replaced by your class/nib name.
----
To copy it to a new project, create the app as above.
But really you probably will be inserting the view into a subview of some view you built

Jerry Isdale
[EMAIL PROTECTED]



_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to