Passing an object to a custom sheet
Passing an object to a custom sheet
- Subject: Passing an object to a custom sheet
- From: Torsten Geise <email@hidden>
- Date: Mon, 04 Apr 2011 22:29:22 +0200
I successfully created a dialog by following the custom sheet examples. But I still have trouble passing arguments to the dialog. I'll explain my problem based on a simple test app:
- Main.xib contains a window and a button "show dialog".
- AppDelegate has an IBAction for the "show dialog button".
- Dialog.xib contains a window which has a NSTextField "message" and an OK button.
- DialogController is of type NSObject and acts a the File's Owner of Dialog.xib.
- DialogController contains an outlet for the NSTextfield and an IBAction for the OK button. In addition, it has a function that brings up the dialog.
- AppDelegate has a property of type DialogController.
The DialogController interface is defined as follows:
@interface DialogController : NSObject {
@private
NSTextField *messageTextfield;
NSButton *okButton;
NSWindow *window;
}
@property (assign) IBOutlet NSWindow *window;
@property (assign) IBOutlet NSButton *okButton;
@property (assign) IBOutlet NSTextField *messageTextfield;
- (IBAction)onOkButtonPressed:(id)sender;
-(void)showDialog:(NSWindow*)parent;
@end
When one clicks the "show dialog" button, the DialogController:showDialogInParent is called, which looks as follows:
-(void)showDialog:(NSWindow*)parent {
if (!window) {
[NSBundle loadNibNamed:@"Dialog" owner:self];
}
[NSApp beginSheet:window
modalForWindow:parent
modalDelegate:self
didEndSelector:@selector(didEndSheet:returnCode:contextInfo:)
contextInfo:nil];
}
When OK button of the Dialog is clicked, the following function is called:
- (IBAction)onOkButtonPressed:(id)sender {
[NSApp endSheet:window returnCode:NSOKButton];
}
This finally calls the didEndSheet function earlier defined in the NSApp:beginSheet function:
- (void)didEndSheet:(NSWindow *)aSheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo
{
[aSheet orderOut:self];
}
This all works fine. The dialog shows up attached to the parent window, and once the OK button is clicked, it disappears.
So far, so good. My problem starts with passing objects to the dialog or passing the content of the NSTextfield back to the caller. In the showDialog function, I already have an instance to DialogController (instance 1). When the NIB file gets loaded, it creates a second instance of DialogController (instance 2) but containing the same instance of "window".
When I want to use an Object in the "onOkButtonPressed" function, how can I set this object in instance #2? [NSBundle loadNibNamed:@"Dialog" owner:self] does not return a pointer to the instance. In opposite, when "didEndSheet" is called, I'm back in instance#1 without any knowledge about the value of NSTextfield of instance#2.
As I'm trying to get this running for many hours and couldn't find any hints neither in the net nor in Apple's documentation, I'm asking myself whether this is the correct approach at all?
Simple said, I want to have a dialog similar to FileOpenDialog, which returns some values selected in the dialog to the caller, like this:
NSOpenPanel* openPanel = [NSOpenPanel openPanel];
...set some properties of the panel...
if ([openPanel runModal] == NSFileHandlingPanelOKButton) {
NSArray* selectedFiles = [openPanel filenames];
...
}
Thanks,
Torsten
_______________________________________________
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