I’m updating an old project which used the -[NSApp beginSheet:::::] methods to 
use -[NSWindow beginSheet:completionHandler:].  In so doing, I now need the 
parent window in my action methods, so I can send the -[NSWindow endSheet::] 
message which runs the completion handler.  In many cases, the sheet is handy 
but the parent window is not.  No problem, I thought, just use send -[NSWindow 
parentWindow] to the sheet.

Unfortunately this always returns nil, unless I set is using -[NSWindow 
setParentWindow:] beforehand, which the documentation and common sense says is 
unnecessary.

Is the non-functioning of -[NSWindow parentWindow] a known bug, or am I doing 
something wrong?

I have reproduced the problem in a demo project which and put it on github.com 
(links below).  Here is the code:

@interface AppDelegate ()

@property (weak) IBOutlet NSWindow *window;
@property (weak) IBOutlet NSWindow *sheet;

@end

@implementation AppDelegate

- (IBAction)showSheet:(id)sender {
    [self.window beginSheet:self.sheet
     completionHandler:^void(
                             NSModalResponse modalResponse) {
         NSLog(@"Got response: %ld", modalResponse) ;
     }] ;
}

- (IBAction)done:(id)sender {
    NSWindow* parentWindow = [self.sheet parentWindow] ;
    if (!parentWindow) {
        NSLog(@"Nil parentWindow!  Workaround being used") ;
        parentWindow = self.window ;
    }
    [parentWindow endSheet:self.sheet
                returnCode:123] ;
}

@end


Project: https://github.com/jerrykrinock/SheetDemo
Clone:  https://github.com/jerrykrinock/SheetDemo.git
_______________________________________________

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

This email sent to arch...@mail-archive.com

Reply via email to