Re: delegate of an NWindowController's window
Re: delegate of an NWindowController's window
- Subject: Re: delegate of an NWindowController's window
- From: Graham Cox <email@hidden>
- Date: Fri, 30 Sep 2011 10:51:47 +1000
You're overthinking the problem.
NSWindowController doesn't have a 'delegate' method or property, which is the cause of the warning you are receiving.
Using a delegate to handle the result from a sheet is reasonable - it's what I invariably do. But you have to add that delegate property yourself in your NSWindowController subclass. This can be a simple assigned ivar:
@interface MyWindowController : NSWindowController
{
id delegate;
}
@property (assign) id delegate;
@end
I would also not bother with a formal protocol for the delegate callback. Since you are checking whether the delegate responds to the selector, it may as well be an informal protocol, which is easier to set up.
Informal protocols are declared using a category on (usually) NSObject:
@interface NSObject (MyWindowControllerDelegate)
- (void) windowController:(MyWindowController*) controller insertData:(NSDictionary*) dict;
@end
This also shows you two tips:
a) pass the controller that is doing the setting - there may come a time that you want to tell apart different ones, or request other information from the controller.
b) pass the data as a dictionary, not a mutable dictionary. The client should not be given any opportunity to mutate things that it has no business mutating.
--Graham
On 30/09/2011, at 10:38 AM, Koen van der Drift wrote:
> Thanks for the response, but, still not working.
>
>
> Here's in more detail what I'm trying to do. My main window is controlled by MyAppDelegate:
>
> @interface MyAppDelegate : NSObject <NSApplicationDelegate>
>
>
> In MyAppDelegate I respond to an IBAction to open a second window to generate some data that needs to be go back to MyAppDelegate to put in my model. The second window is controlled by MyDataWindowController, a subclass of NSWindowController.
>
> - (IBAction)showDataWindow:(id)sender
> {
> MyDataWindowController *searchDataWindow = [[MyDataWindowController alloc]
> initWithWindowNibName: @"searchDataWindow"];
> [NSApp beginSheet:[searchDataWindow window]
> modalForWindow:[self window]
> modalDelegate: self
> didEndSelector: nil
> contextInfo: NULL];
> }
>
> This all goes fine, the sheet opens and I get generate the data, stored in an NSDictionary in MyDataWindowController. Now I want somehow to get the data back to MyAppDelegate. So I am trying this in MyDataWindowController:
>
> At the top of MyDataWindowController I add this @protocol:
>
> @protocol MyDataWindowControllerDelegate <NSObject>
> - (void)insertData: (NSMutableDictionary *)data;
> @end
>
> and then after the data is obtained:
>
> id <MyDataWindowControllerDelegate> del = [self delegate]; // <-------
>
> if ([del respondsToSelector:@selector(insertData:)])
> {
> [del insertData: data];
> }
> }
>
> [NSApp endSheet: [self window] returnCode: 1];
> [[self window] orderOut: self];
>
> Which now gives the warning:
>
> Semantic Issue: Instance method '-delegate' not found (return type defaults to 'id') on the line with the arrow.
>
>
>
> Any suggestions how to make this work? Maybe not use a delegate?
>
>
> Thanks,
>
> - Koen.
>
>
>
>
>
>
> _______________________________________________
>
> 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