Cocoa: NSApp beginSheet sets the application delegate?

那年仲夏 提交于 2019-12-11 01:21:40

问题


I am trying to display a custom sheet in my application, but I think I am doing something wrong. While everything seems to be working just fine, I have a rather odd side-effect. (which took hours to figure out). It turns out that everytime I display a sheet in my application, the Application delegate gets set to the instance of the sheet, thus my Controller gets unset as the delegate causing all sorts of problems.

I've created a NIB file which I called FailureSheet.xib. I laid out my interface in IB, and then created a subclass of 'NSWindowController' called 'FailureSheet.m' which I set to the File's Owner. Here is my FailureSheet class:

#import "FailureSheet.h"

@implementation FailureSheet  // extends NSWindowController

- (id)init
{
    if (self = [super initWithWindowNibName:@"FailureSheet" owner:self])
    {

    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (IBAction)closeSheetTryAgain:(id)sender
{   
    [NSApp endSheet:[self window] returnCode:1];
    [[self window] orderOut:nil];
}

- (IBAction)closeSheetCancel:(id)sender
{
    [NSApp endSheet:[self window] returnCode:0];
    [[self window] orderOut:nil];
}

- (IBAction)closeSheetCancelAll:(id)sender
{
    [NSApp endSheet:[self window] returnCode:-1];
    [[self window] orderOut:nil];
}

@end

Nothing complex going on here. Now this is how I display the FailureSheet in my 'Controller' class:

sheet = [[FailureSheet alloc] init];

[NSApp beginSheet:[sheet window]
   modalForWindow:window
    modalDelegate:self
   didEndSelector:@selector(failureSheetDidEnd:etc:etc:)
      contextInfo:nil];

Now if I log what the [NSApp delegate] is before displaying my sheet, it is <Controller-0x012345> which is correct. Then, after running this code and my sheet is up, if I log it again it is <FailureSheet-0xABCDEF>.

Not sure what I'm doing wrong here - Any ideas?


回答1:


This is one of those "I'm-an-idiot" answers.

Turns out I at some point I accidentally made a connection in my sheet's NIB file between the Application and the File's Owner (FailureSheet) setting it as the delegate. So, everytime it got loaded it overwrote the existing delegate connection I had in my MainMenu NIB file.



来源:https://stackoverflow.com/questions/2463435/cocoa-nsapp-beginsheet-sets-the-application-delegate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!