Re: Problem with multiple instances of a nib
Re: Problem with multiple instances of a nib
- Subject: Re: Problem with multiple instances of a nib
- From: PCWiz <email@hidden>
- Date: Sun, 22 Nov 2009 18:42:20 -0700
Ok, I have fixed this problem. Here is the solution:
First of all, you would need to keep a reference to the controller
object inside the panel itself. I'm not sure if there is a cleaner way
to do it, but I found that subclassing your panel and then adding a
property to keep a reference to the controller worked. Like this:
//// CustomPanel.h ////
@class CustomPanelController;
@interface CustomPanel : NSPanel {
id ctrl;
}
@property (readwrite, retain) id ctrl;
@end
//// CustomPanel.m ////
@implementation CustomPanel
@synthesize ctrl;
@end
When you create a new instance of the controller, you need to put
something like this in the init method:
newPanel.ctrl = self;
Where newPanel would be an outlet to your NSPanel.
Create a controller manager class (just something to keep references
to the controllers):
//// ControllerManager.h ////
@interface ControllerManager : NSObject {
NSMutableArray *controllers;
}
@property(readwrite, retain) NSMutableArray *controllers;
+ (id)sharedInstance;
- (void)addController:(id)controller;
- (void)removeController:(id)controller;
@end
//// ControllerManager.m ////
@implementation ControllerManager
@synthesize controllers;
+ (id)sharedInstance {
static id sharedInstance = nil;
if (sharedInstance == nil) {
sharedInstance = [[self alloc] init];
}
return sharedInstance;
}
- (id)init
{
if (self = [super init])
{
controllers = [[NSMutableArray alloc] init];
}
return self;
}
- (void)dealloc
{
if (controllers) [controllers release];
[super dealloc];
}
- (void)addController:(id)controller
{
[controllers addObject:controller];
}
- (void)removeController:(id)controller
{
[controllers removeObject:controller];
}
- (void)windowWillClose:(NSNotification *)aNotification
{
CustomPanel *panel = (CustomPanel*)[aNotification object];
[controllers removeObject:[panel ctrl]];
}
@end
Also in your controller's init method you should set the delegate of
your panel to the ControllerManager:
[newPanel setDelegate:[ControllerManager sharedInstance]];
I hope this helps. If there's any issues in my code please feel free
to correct me. I have tried this in my app and it seems to work fine.
On 2009-11-22, at 3:58 PM, Rob Keniger wrote:
On 23/11/2009, at 8:49 AM, PCWiz wrote:
Good to know that I'm not the only one experiencing this. The
easiest solution I guess is to keep an array of the controller
objects, which I will try. Is this how you mean:
NSMutableArray *controllerObjects = [[NSMutableArray alloc] init];
ImagePanelController *newController = [[[ImagePanelController
alloc] init] autorelease];
[controllerObjects addObject:newController];
With this method, when the panel is closed would the controller be
removed from the array (I'm guessing not, and if so, how would I
make it do that?)
Register the object that manages the array as a delegate of the
window, implement -windowWillClose: and remove the appropriate
window controller from the array?
--
Rob Keniger
_______________________________________________
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
_______________________________________________
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